Skip to content

Operators

Operators combine or compare values. JavaScript groups them into arithmetic (+, -, *, /, %, **), comparison (==, ===, <, >=, etc.), logical (&&, ||, !), and a handful of newer, more precise operators like ?? (nullish coalescing) and ?. (optional chaining).

console.log(10 % 3); // 1 -- remainder
console.log(2 ** 8); // 256 -- exponentiation
console.log(5 == "5"); // true -- loose equality, coerces types
console.log(5 === "5"); // false -- strict equality, no coercion
console.log(true && "yes"); // "yes" -- && returns the last truthy operand
console.log(false || "default"); // "default"
const port = process.env.PORT ?? 3000; // only falls back for null/undefined
const city = user?.address?.city; // safely reads nested, possibly-missing props

Using == (loose equality) instead of === (strict equality) β€” == performs type coercion before comparing, producing surprising results for edge cases.

console.log(0 == ""); // true -- both coerce to falsy
console.log(0 == "0"); // true
console.log("" == "0"); // false -- inconsistent, hard to predict
console.log(null == undefined); // true
// Strict equality avoids all of this coercion
console.log(0 === ""); // false
console.log(0 === "0"); // false
  1. What’s the key difference between == and ===?

    Answer== coerces operands to the same type before comparing; === compares both value and type with no coercion. === is almost always the safer choice.
  2. What does ?? do differently from ||?

    Answer?? only falls back on null or undefined; || falls back on any falsy value, including 0, "", and false, which is often not what you want.
  3. What does user?.address?.city return if user.address is undefined?

    Answerundefined β€” optional chaining short-circuits and returns undefined instead of throwing a TypeError when it hits a null/undefined link in the chain.