SkillPilotAI

Operators, equality and truthy/falsy

Operators look simple, but JavaScript quietly converts types: "500" + 90 gives "50090", not 590. This note covers type coercion, == vs ===, truthy and falsy values, and the modern operators ??, ?. and the ternary.

3.1 Problem: string plus number

  • price comes from a form input box as a string, gst is a normal number.
  • If + gets even one string, it joins instead of adding: "500" + 90 = "50090".

3.2 Type coercion

  • - and * only work with numbers, so JavaScript converts the string to a number automatically (410, 1000).
  • This automatic conversion is called type coercion.
  • Fix: convert yourself with Number() before calculating, then the total is 590.
JavaScript
const price = "500";const gst = 90; console.log(price + gst);console.log(price - gst);console.log(price * 2);console.log(Number(price) + gst);
Output
> 50090> 410> 1000> 590

3.3 == vs ===

  • == (loose equality) converts the types first and then compares: "5" == 5 is true.
  • === (strict equality) checks type and value with no conversion: "5" === 5 is false.
  • 0 == "" is true and null == undefined is true; with === both are false.
JavaScript
console.log("5" == 5);console.log("5" === 5); console.log(0 == "");console.log(0 === ""); console.log(null == undefined);console.log(null === undefined);
Output
> true> false> true> false> true> false
What == does inside
  1. 1.Compare "5" == 5

  2. 2.Different types? string and number

  3. 3.Convert to a common type (usually number): "5" becomes 5

  4. 4.Compare: 5 == 5 is true

  5. 5.=== skips the conversion: types differ, so false

The conversion rules are surprising, so prefer ===

Expression=====
"5" vs 5truefalse
0 vs ""truefalse
null vs undefinedtruefalse

3.4 Truthy and falsy

  • Inside an if, JavaScript turns any value into true or false.
  • Falsy values (short list): false, 0, "" (empty string), null, undefined, NaN.
  • -0 and 0n (BigInt zero) are also falsy, but rare in real code.
  • Every other value is truthy, including [] and {}.
JavaScript
const cart = []; if (cart) {  console.log("Show checkout");} else {  console.log("Cart is empty");}
Output
> Show checkout
  • Checkpoint: an empty array is truthy, so "Show checkout" prints even for an empty cart.
  • To check for an empty cart, test cart.length > 0.

3.5 Default values: || vs ??

  • qty || 1: 0 is falsy, so || ignores it and gives 1 even though the user really chose 0.
  • qty ?? 1 (nullish coalescing) uses the default only for null or undefined, so 0 stays 0.
JavaScript
// Wrongconst qty = 0;const shown = qty || 1;console.log(shown); // 1
JavaScript
// Rightconst qty = 0;const shown = qty ?? 1;console.log(shown); // 0

3.6 Optional chaining ?.

  • User data from an API sometimes has no address: reading user.address.city on null crashes with a TypeError.
  • user.address?.city stops at address if it is null or undefined and returns undefined instead of crashing.
  • Combine with ??: the console prints "Not set" and the app keeps running.
JavaScript
const user = { name: "Riya", address: null }; console.log(user.address.city);
Output
TypeError: Cannot read properties of null
JavaScript
const user = { name: "Riya", address: null }; const city = user.address?.city ?? "Not set";console.log(city);
Output
> Not set
How user.address?.city ?? "Not set" is evaluated
  1. 1.Read user.address

  2. 2.Is it null or undefined? Yes: stop, result undefined (no crash)

  3. 3.Otherwise read .city

  4. 4.?? checks the result: null / undefined?

  5. 5.Yes: use "Not set"; no: keep the city

3.7 Ternary operator

  • A short if / else that picks one of two values in one line: condition ? valueIfTrue : valueIfFalse.
  • qty is 0, so it prints "Sold out".
  • In React the ternary is the most common way to show conditional UI.
JavaScript
const user = { name: "Riya", address: null }; const city = user.address?.city ?? "Not set";console.log(city); const qty = 0;const msg = qty > 0 ? "In stock" : "Sold out";console.log(msg);
Output
> Not set> Sold out

3.8 Common mistakes

  • Thinking === is always perfect: NaN === NaN is false, so use Number.isNaN().
  • Objects and arrays compare by reference: [] === [] is false.
  • Using || for defaults when 0 or "" is a valid value; use ?? instead.
  • Checking if (cart) for an empty array; check cart.length instead.
  • Nested ternaries: hard to read, avoid them.

3.9 Rules

  • Always use ===; write == only when you intentionally want null and undefined together.
  • Defaults: ?? whenever 0 or "" can be valid.
  • Use ?. only where data can genuinely be missing.

3.10 Interview tips

  • == does type coercion before comparing; === checks type and value with no conversion.
  • Example: "5" == 5 is true, "5" === 5 is false; say you always use ===.
  • Bonus: || falls back on any falsy value, ?? only on null / undefined.
  • Know the falsy list by heart, and that [] and {} are truthy.