Sub topic 14
Inside Loose Equality (==)
This chapter assumes you already know strict equality (===), ToNumber, and ToPrimitive from earlier chapters. Loose equality looks unpredictable, but the specification defines it as a short, fixed algorithm called IsLooselyEqual. Once you know its steps, you can trace any == result by hand.
The algorithm, step by step
When you evaluate x == y, JavaScript runs these checks in order and stops at the first one that applies:
- Same type: return the result of
x === y. - null and undefined: if one is
nulland the other isundefined, returntrue. - Number and String: convert the string to a number with ToNumber, then compare again.
- BigInt and String: convert the string to a BigInt. If that fails, return
false. - Boolean on either side: convert the boolean to a number (
truebecomes 1,falsebecomes 0), then compare again. - Object and primitive: convert the object with ToPrimitive (default hint), then compare again.
- BigInt and Number: compare their mathematical values.
- Anything else: return
false.
Steps 3 through 6 call the algorithm again with the converted values, so one comparison can pass through several steps.
Tracing the classic cases
console.log(null == undefined); // true: step 2
console.log(null == 0); // false: no step converts null, so step 8
console.log("" == 0); // true: "" becomes 0
console.log("1" == 1); // true: "1" becomes 1
console.log("0" == false); // true: false becomes 0, then "0" becomes 0
console.log([1] == 1); // true: [1] becomes "1", then 1
console.log([] == false); // true: false becomes 0, [] becomes "", "" becomes 0
console.log([] == ![]); // true
console.log(NaN == NaN); // false: step 1, and NaN === NaN is falseHere is the full trace for [] == ![]:
| Step | Expression | Rule applied |
|---|---|---|
| 0 | `[] == ![]` | `![]` is `false` because every object is truthy |
| 1 | `[] == false` | Boolean becomes a number |
| 2 | `[] == 0` | ToPrimitive turns `[]` into `""` |
| 3 | `"" == 0` | String becomes a number |
| 4 | `0 == 0` | Same type, so the result is `true` |
Common errors
- Assuming
null == 0:nullloosely equals onlyundefined. Its only other match is the legacy browser objectdocument.all. - Comparing two objects: both operands are objects, so step 1 applies and the comparison checks identity.
[1] == [1]isfalse. - Expecting
NaNto match anything: a comparison that reachesNaNalways returnsfalse.
The one common idiom that relies on == is value == null. It is true for both null and undefined and false for everything else.
Exercise
Trace "1" == true step by step and give the result. Then explain why "2" == true is false.
Show answer
true is a boolean, so it becomes 1, which gives "1" == 1. That is a string compared with a number, so "1" becomes 1, which gives 1 == 1, true. For "2" == true, true becomes 1 and "2" becomes 2, so the comparison is 2 == 1, which is false. A truthy string does not loosely equal true unless it converts to the number 1.
Exercise
What does [0] == false return, and which steps run?
Show answer
true. false becomes 0 (boolean step), giving [0] == 0. ToPrimitive turns [0] into "0" (object step), giving "0" == 0. The string becomes 0, and 0 == 0 is true.