JavaScript Strict vs Loose Equality: Coercion Rules and Pitfalls
StringsModel answerLast updated
Explain how JavaScript's two equality operators work, how implicit type coercion drives loose equality, where results diverge, and which operator to prefer.
JavaScript has two ways to test whether values are equal: the double-equals form (==) and the triple-equals form (===).
Write an answer that covers the following:
- Describe the steps each operator takes when it compares two values.
- Define implicit type coercion and explain how it connects to
==. - Give concrete expressions where the two operators return different results, and explain each result. Include at least one case where both operators return
falseeven though the result may look surprising. - State which operator you would use by default on a team, justify the choice, and mention any situation where you would deliberately choose the other one.
Bonus points for discussing how objects are compared, what happens with NaN and signed zero, and whether == is transitive.
Hints
Hint 1
Start with the simplest rule: `===` never converts anything. If the two operands have different types it answers `false` right away. Then ask what `==` does differently when the types differ.
Hint 2
For `==`, remember the order of conversions: `null` and `undefined` are special, strings convert to numbers, booleans convert to numbers (not the other way round), and objects convert to primitives before the comparison is retried.
Hint 3
Test your understanding on `[] == false`, `"2" == true`, `null == 0` and `"" == "0"`. Work out each result by applying one conversion at a time, and notice what this reveals about transitivity.
Model answer
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Short answer
=== (strict equality) compares without converting. Different types mean false. Same type means compare values, or references for objects.
== (loose equality) does the same thing when both types match. When the types differ, it first converts the operands toward a common type (type coercion) and then compares. Because those conversion rules are irregular, == produces many surprising results, so the default recommendation is to use ===.
How each operator works
Strict equality, a === b
- If the types differ, the result is
false. No conversion happens. - Numbers:
NaN === NaNisfalse, and0 === -0istrue. - Strings, booleans,
null,undefined: compared by value. - Objects, arrays and functions:
trueonly if both sides are the same reference.
Loose equality, a == b (simplified from the spec's abstract equality algorithm)
- Same type: behave exactly like
===. null == undefinedistrue. Neither equals anything else, sonull == 0isfalse.- Number vs string: convert the string to a number, then compare.
- Boolean vs anything: convert the boolean to a number (
truebecomes1,falsebecomes0) and compare again. - Object vs primitive: convert the object to a primitive (via
valueOf/toString) and compare again. - BigInt vs number or string: compare mathematical values.
- Anything else:
false.
What type coercion is
Type coercion is the implicit conversion of a value from one type to another, performed by the language rather than by an explicit call such as Number(x). Loose equality is one of the places where it happens (arithmetic with +, and if conditions, are others).
Useful conversion facts:
""and whitespace-only strings convert to0."0x10"converts to16.[]converts to"", which converts to0.[1]converts to"1", which converts to1.
A key point that candidates often get wrong: booleans are converted to numbers, not the other way round. So x == true is not a truthiness test. It checks whether x coerces to 1.
Examples where the operators differ
| Expression | == | === | Reasoning for == |
|---|---|---|---|
"1" == 1 | true | false | "1" becomes 1 |
0 == "" | true | false | "" becomes 0 |
"0" == false | true | false | false becomes 0, "0" becomes 0 |
null == undefined | true | false | special rule |
[] == false | true | false | false becomes 0; [] becomes "" then 0 |
[] == ![] | true | false | ![] is false, then same as above |
[1,2] == "1,2" | true | false | the array converts to "1,2" |
Cases where both return false:
| Expression | Why |
|---|---|
null == 0 | null only loosely equals undefined |
"2" == true | true becomes 1, "2" becomes 2, and 2 !== 1 |
NaN == NaN | NaN is not equal to anything, including itself |
[1,2] == [1,2] | two distinct objects, compared by reference |
Transitivity breaks
"" == 0 // true
0 == "0" // true
"" == "0" // false
Equality that is not transitive makes reasoning about code and about collections of mixed values hard.
Objects and references
Both operators compare objects by identity. {} === {} and {} == {} are both false, while const o = {}; o === o is true. Loose equality only converts an object when the other side is a primitive. This also means == can invoke user-defined valueOf/toString, which can run arbitrary code with side effects.
NaN and signed zero
=== treats NaN as unequal to itself and 0 as equal to -0. If you need the opposite behaviour, use Object.is(a, b): Object.is(NaN, NaN) is true and Object.is(0, -0) is false. For detecting NaN specifically, prefer Number.isNaN(x).
Recommendation
Use === and !== by default, because:
- Behaviour depends only on the values and their types, not on a conversion table you have to remember.
- Bugs such as
if (input == 0)accidentally matching"","0"or[]disappear. - Code reviewers can read intent without simulating coercion.
- Linters can enforce it (for example ESLint's
eqeqeqrule).
Deliberate exception: x == null is a compact way to test "x is null or undefined" and nothing else. Many style guides allow this one idiom (ESLint's eqeqeq has a "null": "ignore" option for it). Even then, an explicit x === null || x === undefined is fine if the team prefers zero exceptions.
When the data comes in as a string (form input, query params), convert explicitly before comparing: Number(input) === 5. This makes the conversion visible and lets you handle invalid input (Number.isNaN) intentionally.
Complexity
===is O(1) for numbers, booleans and references, and O(n) for strings of length n.==performs a small, bounded number of conversion steps plus O(n) string parsing when needed. The hidden cost is the possibility of user-defined conversion methods running.
Common mistakes
- Believing
==is "===but ignoring type". It is a specific sequence of conversions, and several of them are non-obvious. - Thinking
x == truetests truthiness. It does not; useBoolean(x)or justif (x)if that is what you mean. - Assuming
null == 0istruebecausenullis "falsy". - Expecting arrays or objects with equal contents to compare as equal.
- Using
===to detectNaN. - Forgetting that
==can be non-transitive and so cannot be safely used for sorting, deduplication or as a map key replacement.