JavaScript equality and type coercion: == vs ===, truthy and falsy
Why 0 == '' is true, which values are falsy, how + turns numbers into strings, and the handful of rules that make JavaScript comparisons predictable.
JavaScript tries hard to make your code "work", even when the types don't match. That's what type coercion is: the engine quietly converts a value to another type so an operation can run. It's convenient right up to the moment it isn't.
== converts, === doesn't
== (loose equality) converts both sides to a common type before comparing. === (strict
equality) returns false as soon as the types differ.
0 == ""; // true ("" becomes 0)
0 == "0"; // true ("0" becomes 0)
"" == "0"; // false (both strings, different text)
null == undefined; // true
null == 0; // false
0 === ""; // false: number vs stringNotice that == isn't even transitive: 0 == "" and 0 == "0", but "" != "0". That's reason
enough to use === everywhere.
The one useful exception: value == null is true for both null and undefined and nothing
else. Many style guides allow exactly this pattern.
Truthy and falsy
if, while, &&, || and ! convert their operand to a boolean. These eight values become
false; everything else becomes true:
| Falsy values |
|---|
false, 0, -0, 0n, "", null, undefined, NaN |
So "0", "false", [] and {} are all truthy. A classic bug:
const seats = 0;
if (!seats) console.log("No seats info"); // runs, but 0 seats is valid information!
if (seats === undefined) console.log("No seats info"); // what you meant|| versus ??
a || b returns b whenever a is falsy. a ?? b (nullish coalescing) returns b only when
a is null or undefined:
const volume = 0;
volume || 50; // 50: the user's 0 is lost
volume ?? 50; // 0: only null/undefined fall backUse ?? for defaults; use || only when you truly want to replace every falsy value.
+ is addition and concatenation
If either side of + is a string, the other side becomes a string:
1 + 2; // 3
"1" + 2; // "12"
1 + 2 + "3"; // "33" (left to right: 3, then "33")
"5" - 2; // 3: minus only means subtraction, so "5" becomes 5Input from prompt(), form fields and URLs always arrives as a string. Convert it explicitly,
once, at the edge of your program:
const seats = Number(prompt("How many seats?")); // NaN if it isn't a number
if (Number.isNaN(seats)) throw new Error("Not a number");NaN is never equal to anything
NaN === NaN is false. Test for it with Number.isNaN(x), not x === NaN. (The older global
isNaN("abc") converts first and returns true for any non-numeric string; prefer the
Number. version.)
Comparing objects and arrays
=== on objects compares identity, not contents:
[1, 2] === [1, 2]; // false: two different arrays
const a = [1, 2];
a === a; // true: same arrayTo compare contents, compare the parts (or, for simple data, JSON.stringify both sides, keeping
in mind that key order matters).
The rules to remember
- Use
===and!==; allow== nullif your team does. - Convert input with
Number()orparseInt(x, 10)as soon as you read it. - Use
??for defaults,Number.isNaNfor NaN checks. - Remember that
[],{}and"0"are truthy.
You practise exactly these rules from the first module of JavaScript Fundamentals.
Questions and answers
- Should I always use === in JavaScript?
- Yes, as a default. === compares without converting types, so 0 === '' is false. The one common exception is value == null, which is a deliberate shorthand for 'null or undefined'.
- Which values are falsy in JavaScript?
- false, 0, -0, 0n, '' (the empty string), null, undefined and NaN. Everything else is truthy, including '0', 'false', [] and {}.
Practise it
Read next
- JavaScript arrays: map, filter, reduce, find and sort without surprises
The array methods you use every day, which ones change the array and which return a new one, and the sort() bug that bites everyone who sorts numbers.
- JavaScript closures and scope explained with small examples
What a closure is, why let and const are block-scoped while var isn't, the classic loop bug, and how closures give you private state, counters and memoised functions.