An array that equals its own opposite
Open a browser console and type:
[] == ![]It says true. An empty array is, apparently, equal to not an empty array.
This is not a bug in the language. Every step follows a written rule. The surprise is in how many steps there are, and that you cannot see any of them.
What == does
JavaScript has two equality operators.
===asks: same type and same value? If the types differ, the answer isfalsestraight away.==asks: are these equal after converting them to the same type? That automatic conversion is called type coercion.
When == meets two different types, it does not give up. It follows a fixed list of rules to convert one or both sides, and tries again. Sometimes that takes several rounds.
Following the rules, one at a time
[] == ![]the starting point
Start. An empty array compared with "not an empty array". It looks as if this must be false. The == operator converts both sides until they are the same type, so follow each conversion.
Work out ![] first
!converts its value to a boolean and flips it. Every object is "truthy", an empty array included. So![]isfalse. Now we have[] == false.A boolean in == becomes a number
falsebecomes0. Now[] == 0.An object in == becomes a primitive
An array is converted by joining its items with commas. No items gives the empty string. Now
"" == 0.A string against a number becomes a number
The empty string converts to
0. Now0 == 0.Same type: compare normally
0 == 0istrue.
Both sides ended up as the number 0 by completely different routes.
The two ideas hiding in there
Truthy and falsy. When a value is used as a condition, it is treated as true or false. Only a handful of values are falsy:
false, 0, "", null, undefined, NaN
Everything else is truthy, including [], {}, "0" and "false".
Converting to a boolean is not the same as converting with ==. if ([]) asks "is this truthy?", and the answer is yes. [] == false runs the conversion rules above, and the answer is also yes. An empty array is truthy and loosely equal to false. Those are two different questions.
More from the same rulebook
| Expression | Result | Why |
|---|---|---|
"5" == 5 | true | The string becomes the number 5 |
0 == "" | true | The empty string becomes 0 |
0 == "0" | true | "0" becomes 0 |
"" == "0" | false | Both are strings, and they differ. So == is not consistent: a equals b, a equals c, but b does not equal c |
null == undefined | true | A special rule: these two equal each other and nothing else |
null == 0 | false | That same special rule |
NaN == NaN | false | NaN is not equal to anything, itself included |
"1" + 1 | "11" | + with a string joins text |
"3" - 1 | 2 | - only works on numbers, so the string is converted |
[1, 2] + [3] | "1,23" | Both arrays become strings, then are joined |
What to do about it
Use === and !== everywhere
They never convert. Different types are simply not equal. Code checkers enforce this by default.
Convert on purpose, where you can see it
Number(input),String(value),Boolean(value). The conversion still happens, but now it is written down.Know the one accepted use of ==
value == nullis true for bothnullandundefinedand nothing else. Some codebases allow exactly that.Check for emptiness explicitly
items.length === 0says what it means.if (!items)does not: an empty array is truthy.
Key takeaways
- == converts both sides to a common type before comparing. === does not.
- The conversions follow fixed rules: booleans to numbers, objects to strings, strings to numbers.
- [] == ![] is true because both sides end up as the number 0.
- Truthiness and == are different questions: an empty array is truthy and also loosely equal to false.
- Use === and convert types explicitly.