Learn all JavaScript operators — arithmetic, assignment, comparison, logical, bitwise, ternary, optional chaining, and nullish coalescing.
Why: arithmetic operators perform math. Assignment operators (+=, -=, etc.) combine a math operation with assignment in one step.
let x = 10;
// Arithmetic
console.log(x + 3); // 13
console.log(x - 3); // 7
console.log(x * 3); // 30
console.log(x / 3); // 3.333...
console.log(x % 3); // 1 (remainder)
console.log(x ** 2); // 100 (exponentiation)
// Assignment shortcuts
x += 5; console.log(x); // 15
x -= 5; console.log(x); // 10
x *= 2; console.log(x); // 20
x /= 4; console.log(x); // 5
x **= 3; console.log(x); // 125
// Increment / decrement
let n = 5;
console.log(n++); // 5 (post: returns then increments)
console.log(n); // 6
console.log(++n); // 7 (pre: increments then returns)Why: comparison operators return booleans. Logical operators (&&, ||, ??) combine booleans and short-circuit — useful for conditional defaults.
// Comparison
console.log(5 > 3); // true
console.log(5 >= 5); // true
console.log('a' < 'b'); // true (lexicographic)
// Logical AND / OR (short-circuit)
const user = null;
const name = user && user.name; // doesn't throw
console.log(name); // null
const display = user || 'Guest'; // fallback on falsy
console.log(display); // 'Guest'
// Nullish coalescing — only falls back on null/undefined
const val = 0 ?? 'default';
console.log(val); // 0 (unlike ||, 0 is not nullish)
// Optional chaining — safe property access
const config = { db: { host: 'localhost' } };
console.log(config?.db?.host); // 'localhost'
console.log(config?.cache?.ttl); // undefined — no throwWhy: bitwise operators work on 32-bit integers. The ternary operator is a concise if/else. The comma operator evaluates both operands and returns the last.
// Unary
console.log(typeof 'hello'); // 'string'
console.log(-5); // negation
console.log(!true); // false
console.log(void 0); // undefined
// Ternary (conditional)
const age = 20;
const status = age >= 18 ? 'adult' : 'minor';
console.log(status); // 'adult'
// Bitwise (operates on 32-bit integers)
console.log(5 & 3); // 1 (AND)
console.log(5 | 3); // 7 (OR)
console.log(5 ^ 3); // 6 (XOR)
console.log(~5); // -6 (NOT)
console.log(5 << 1); // 10 (left shift)
console.log(20 >> 2); // 5 (right shift)
// Comma operator — evaluates both, returns last
const result = (1 + 1, 2 + 2);
console.log(result); // 4