Operators & Expressions
The building blocks of computation. Combine values, compare data, and control logic.
Arithmetic Operators
Basic math operations on numbers.
Interactive — change values
Addition
10 + 3 = 13
Subtraction
10 - 3 = 7
Multiplication
10 * 3 = 30
Division
10 / 3 = 3.3333
Modulus
10 % 3 = 1
Exponent
10 ** 3 = 1000
// Increment & Decrement let count = 5; count++; // 6 (post-increment) count--; // 5 (post-decrement) ++count; // 6 (pre-increment) // Unary operators let x = "5"; console.log(+x); // 5 (converts to number) console.log(-x); // -5
💡 String + Number: "5" + 3 = "53" (concatenation, not addition). Use Number() or + to convert first.
Assignment Operators
Store and update values in variables.
| Operator | Example | Same As |
|---|---|---|
| = | x = 10 | x = 10 |
| += | x += 5 | x = x + 5 |
| -= | x -= 3 | x = x - 3 |
| *= | x *= 2 | x = x * 2 |
| /= | x /= 4 | x = x / 4 |
| %= | x %= 3 | x = x % 3 |
| **= | x **= 2 | x = x ** 2 |
let score = 0; console.log(score);
Start with score = 0.
Comparison Operators
Compare values and get true or false.
| Expression | Result | Why |
|---|---|---|
| 5 == "5" | true | Loose: converts types |
| 5 === "5" | false | Strict: different types |
| null == undefined | true | Special case in spec |
| null === undefined | false | Different types |
| NaN === NaN | false | NaN is never equal to anything |
🏆 Always use === (strict equality). It checks both value and type, avoiding confusing type coercion.
Logical Operators
Combine conditions with AND, OR, and NOT.
Interactive — toggle values
// Short-circuit evaluation let user = null; let name = user && user.name; // null (stops at falsy) let fallback = name || "Guest"; // "Guest" (finds first truthy) // Nullish coalescing (see next section) let count = 0; let result = count ?? 10; // 0 (only null/undefined trigger ??)
Falsy Values in JavaScript
Everything else is truthy (including "0", "false", [], {}).
Ternary & Nullish Coalescing
Concise conditional expressions.
// Ternary operator: condition ? ifTrue : ifFalse let age = 20; let status = age >= 18 ? "adult" : "minor"; console.log(status);
The ternary operator is a one-line if/else. If the condition is true, return the first value; otherwise the second.
Operator Precedence
Which operations execute first?
| Priority | Operator | Example |
|---|---|---|
| 1 (highest) | ( ) | (2 + 3) * 4 |
| 2 | ** ++ -- ! | 2**3, !true |
| 3 | * / % | 6 * 2 / 3 |
| 4 | + - | 5 + 3 - 1 |
| 5 | < > <= >= | a > b |
| 6 | == != === !== | a === b |
| 7 | && | a && b |
| 8 | || | a || b |
| 9 | ?? | a ?? b |
| 10 (lowest) | = += -= etc. | x = 5 |
// What does this evaluate to? let result = 2 + 3 * 4 ** 2; // Step 1: 4 ** 2 = 16 // Step 2: 3 * 16 = 48 // Step 3: 2 + 48 = 50 console.log(result);
🏆 Best practice: Use parentheses to make complex expressions readable, even when not strictly necessary.
Exercises
Test your understanding.
What does 10 % 3 return?
What does this output?
console.log("5" == 5);What does this output?
console.log("5" === 5);What does true && 0 || 'hello' evaluate to?
Frequently Asked Questions
Common questions about operators.