📘 Lesson · Lesson 12
Operators
Operators
Arithmetic Operators
| Operator | Does | Example |
|---|---|---|
| + | Add (or join strings) | 5 + 3 → 8 |
| - | Subtract | 5 - 3 → 2 |
| * | Multiply | 5 * 3 → 15 |
| / | Divide | 6 / 3 → 2 |
| % | Remainder (modulus) | 10 % 3 → 1 |
| ** | Exponent (power) | 2 ** 3 → 8 |
You met these with numbers. The one to keep front of mind is % (modulus), which gives the remainder — the standard way to test if a number is even: n % 2 === 0. And remember + is dual-purpose: it adds numbers but CONCATENATES strings, so "5" + 3 gives "53", not 8.
Assignment Operators — useful shorthand
let x = 10;
x += 5; // same as: x = x + 5 → 15
x -= 3; // same as: x = x - 3 → 12
x *= 2; // same as: x = x * 2 → 24
x /= 4; // same as: x = x / 4 → 6
let msg = "Hello";
msg += " World"; // works on strings too → "Hello World"
These are shorthand for "do this operation on the variable, and store the result back in it." Writing
total += price is exactly the same as total = total + price, just shorter and clearer about intent. This pattern appears constantly: accumulating a total, decreasing a countdown, doubling a value. += is particularly common — it works for numbers (adding up) and strings (appending text). Once you're used to these, the longer form starts to feel needlessly verbose.Increment and Decrement — ++ and --
let count = 5;
count++; // adds 1 → 6 (same as count += 1)
count--; // takes 1 → 5
// A subtle difference:
let a = 5;
console.log(a++); // prints 5, THEN increments (post-increment)
console.log(a); // 6
let b = 5;
console.log(++b); // increments FIRST, then prints 6 (pre-increment)
++ adds one and -- subtracts one — the most common operation in all of programming, since counters and loops rely on it. You'll see i++ in nearly every for loop you write. There's a subtlety worth knowing: a++ (after the variable) returns the OLD value then increments, while ++a (before) increments first and returns the new value. This trips people up in interviews. In practice, when used alone on its own line (count++;), both behave identically, so don't overthink it early on.Comparison Operators — producing booleans
| Operator | Means | Example |
|---|---|---|
| === | Equal (value AND type) | 5 === 5 → true |
| !== | Not equal (strict) | 5 !== "5" → true |
| == | Loose equal (avoid!) | 5 == "5" → true |
| > < | Greater / less than | 7 > 3 → true |
| >= <= | Greater/less or equal | 5 >= 5 → true |
Always use
=== (triple equals), not ==. The strict === compares both VALUE and TYPE, so 5 === "5" is false (number vs string) — which is what you want. The loose == tries to convert types before comparing, so 5 == "5" is true, leading to confusing bugs. This is one of the most important rules in JavaScript, and a guaranteed interview question. The rule is simple: use === and !== always. There's a whole chapter on comparison coming, but adopt this habit right now.Logical Operators — AND, OR, NOT
let age = 20, hasID = true;
// && (AND) - BOTH must be true
console.log(age >= 18 && hasID); // true
// || (OR) - AT LEAST ONE must be true
console.log(age < 13 || age > 60); // false
// ! (NOT) - flips true to false
console.log(!hasID); // false
Logical operators combine booleans to make more complex decisions.
&& (AND) is true only when BOTH sides are true — "you may enter if you're 18+ AND have ID." || (OR) is true when AT LEAST ONE side is true — "you get a discount if you're under 13 OR over 60." ! (NOT) simply flips a boolean to its opposite. A memory aid: AND is demanding (needs everything), OR is generous (one is enough). These three power every real-world condition, letting you express rules exactly as you'd say them out loud.Exam Corner
Q: What does x += 5 mean? Shorthand for
Q: Difference between === and == ? === compares value AND type (strict); == converts types first — always use ===.
Q: What does % do? Returns the remainder after division.
Q: What's the difference between && and || ? && needs both sides true; || needs at least one true.
Q: What does count++ do? Increases count by 1.
x = x + 5.Q: Difference between === and == ? === compares value AND type (strict); == converts types first — always use ===.
Q: What does % do? Returns the remainder after division.
Q: What's the difference between && and || ? && needs both sides true; || needs at least one true.
Q: What does count++ do? Increases count by 1.
Arithmetic Operators
| Operator | करता है | Example |
|---|---|---|
| + | जोड़ (या strings जोड़ना) | 5 + 3 → 8 |
| - | घटाना | 5 - 3 → 2 |
| * | गुणा | 5 * 3 → 15 |
| / | भाग | 6 / 3 → 2 |
| % | शेषफल (modulus) | 10 % 3 → 1 |
| ** | Exponent (power) | 2 ** 3 → 8 |
ये आप numbers के साथ मिल चुके. सामने रखने लायक है % (modulus), जो शेषफल देता है — number even है test करने का standard तरीका: n % 2 === 0. और याद रखिए + दोहरे मकसद का है: यह numbers जोड़ता है पर strings को CONCATENATE करता है, तो "5" + 3 "53" देता है, 8 नहीं.
Assignment Operators — उपयोगी shorthand
let x = 10;
x += 5; // same as: x = x + 5 → 15
x -= 3; // same as: x = x - 3 → 12
x *= 2; // same as: x = x * 2 → 24
x /= 4; // same as: x = x / 4 → 6
let msg = "Hello";
msg += " World"; // strings par bhi chalta → "Hello World"
ये "variable पर यह operation करो, और नतीजा उसी में वापस store करो" का shorthand हैं.
total += price लिखना बिल्कुल total = total + price जैसा है, बस छोटा और इरादे के बारे में साफ. यह pattern लगातार आता है: total जमा करना, countdown घटाना, value दोगुना करना. += खासकर common है — यह numbers (जोड़ते) और strings (text जोड़ते) दोनों पर चलता है. इनकी आदत पड़ने पर, लंबा रूप बेवजह लंबा लगने लगता है.Increment और Decrement — ++ और --
let count = 5;
count++; // 1 jodta → 6 (count += 1 jaisa)
count--; // 1 ghatata → 5
// Ek sookshm antar:
let a = 5;
console.log(a++); // 5 print, PHIR increment (post-increment)
console.log(a); // 6
let b = 5;
console.log(++b); // PEHLE increment, phir 6 print (pre-increment)
++ एक जोड़ता है और -- एक घटाता है — पूरी programming में सबसे common operation, क्योंकि counters और loops इस पर निर्भर हैं. आप लगभग हर for loop में i++ देखेंगे. एक सूक्ष्मता जानने लायक: a++ (variable के बाद) PURANA value लौटाकर फिर increment करता है, जबकि ++a (पहले) पहले increment करके नया value लौटाता है. यह interviews में लोगों को फंसाता है. व्यवहार में, अकेले अपनी line पर use होने पर (count++;), दोनों एक जैसा व्यवहार करते हैं, तो शुरू में ज़्यादा मत सोचिए.Comparison Operators — booleans बनाते
| Operator | मतलब | Example |
|---|---|---|
| === | बराबर (value AUR type) | 5 === 5 → true |
| !== | बराबर नहीं (strict) | 5 !== "5" → true |
| == | Loose equal (टालिए!) | 5 == "5" → true |
| > < | बड़ा / छोटा | 7 > 3 → true |
| >= <= | बड़ा/छोटा या बराबर | 5 >= 5 → true |
हमेशा
=== (triple equals) use कीजिए, == नहीं. Strict === VALUE और TYPE दोनों की तुलना करता है, तो 5 === "5" false है (number vs string) — जो आप चाहते हैं. Loose == तुलना से पहले types बदलने की कोशिश करता है, तो 5 == "5" true है, उलझाने वाले bugs की ओर ले जाते. यह JavaScript के सबसे ज़रूरी rules में से एक है, और पक्का interview सवाल. Rule सरल है: हमेशा === और !== use कीजिए. Comparison पर पूरा chapter आ रहा है, पर यह आदत अभी अपनाइए.Logical Operators — AND, OR, NOT
let age = 20, hasID = true;
// && (AND) - DONO true hone chahiye
console.log(age >= 18 && hasID); // true
// || (OR) - KAM SE KAM EK true ho
console.log(age < 13 || age > 60); // false
// ! (NOT) - true ko false me palatta
console.log(!hasID); // false
Logical operators booleans को मिलाकर ज़्यादा जटिल फैसले बनाते हैं.
&& (AND) सिर्फ तब true जब DONO पक्ष true हों — "आप अंदर आ सकते हैं अगर 18+ हैं AUR ID है." || (OR) तब true जब KAM SE KAM EK पक्ष true हो — "आपको छूट मिलती है अगर 13 से कम YA 60 से ऊपर हैं." ! (NOT) बस boolean को उसके उल्टे में पलटता है. याद रखने की तरकीब: AND मांग करने वाला है (सब कुछ चाहिए), OR उदार है (एक काफी है). ये तीन हर असली condition चलाते हैं, आपको rules ठीक वैसे व्यक्त करने देते जैसे आप ज़ोर से कहें.Exam Corner
Q: x += 5 का क्या मतलब है?
Q: === और == में अंतर? === value AUR type तुलना करता (strict); == पहले types बदलता — हमेशा === use कीजिए.
Q: % क्या करता है? Division के बाद शेषफल लौटाता है.
Q: && और || में क्या अंतर है? && को दोनों पक्ष true चाहिए; || को कम से कम एक true.
Q: count++ क्या करता है? count को 1 से बढ़ाता है.
x = x + 5 का shorthand.Q: === और == में अंतर? === value AUR type तुलना करता (strict); == पहले types बदलता — हमेशा === use कीजिए.
Q: % क्या करता है? Division के बाद शेषफल लौटाता है.
Q: && और || में क्या अंतर है? && को दोनों पक्ष true चाहिए; || को कम से कम एक true.
Q: count++ क्या करता है? count को 1 से बढ़ाता है.
💻 Live Code Editor
इस पेज का code यहाँ तैयार है — बदलिए और तुरंत नतीजा देखिए. कुछ install किए बिना.
सब कुछ आपके browser में ही चलता है — कोई server, कोई signup नहीं. JavaScript का
console.log देखने के लिए F12 दबाइए.