Skip to main content
FREE Mock interview & resume review with Sumit. Free right now, no payment needed.Book a slot

Equality & Type Coercion — JavaScript interview guide

14 / 23Language EssentialsMedium priorityEasy 15 min

Equality & Type Coercion

Loose equality (==) converts types before comparing; strict equality (===) does not. Knowing the truthy/falsy list avoids many bugs.

See it run

Press Play, or step with Next. Watch the highlighted line, the Call Stack and memory change together.

What JavaScript does with == and +

Execution phaseStep 1/6
script.js
▶console.log("5" + 1);
2console.log("5" - 1);
3console.log(0 == false);
4console.log(0 === false);
5console.log(null == undefined);
6console.log([] == false);
Call Stack
last in, first out
Globalrunning
Memory / Scope
how JS converts it
"5" + 1"5" + "1"
result"51"
Console
›51

What happens:+ with a string on either side means JOIN. The number 1 is converted to "1", so the answer is the string "51".

Tip: click the box and use ← → keys to step, space to play.

Explained simply

Rules to remember

Falsy values: false, 0, -0, 0n, "", null, undefined, NaN. Everything else is truthy, including "0", [] and {}.

null == undefined is true, but null === undefined is false. NaN is not equal to anything, even itself — use Number.isNaN.

"5" + 1   // "51"  (+ with a string concatenates)
"5" - 1   // 4     (- converts to number)
[] + []   // ""
typeof null // "object" (old bug)
Object.is(NaN, NaN) // true

Remember this

  • Prefer === everywhere.
  • typeof null is "object".
  • Use Array.isArray to check arrays.

Interview questions

Why is [] == false true?

Both sides are converted to numbers: [] → "" → 0 and false → 0, so 0 == 0 is true.

Practice

  1. Write a function that returns the real type of any value (null, array, date…).

Keep learning