Yes, [] == ![] Is True. No, JavaScript Isn't Broken (I Checked, Slowly).
Is JavaScript gaslighting me, or is there a logic to this? I hit this line at about 1 a.m., laptop on a pillow, one eye open. I read the result, read it again, and seriously considered that the language was making fun o
Is JavaScript gaslighting me, or is there a logic to this?
I hit this line at about 1 a.m., laptop on a pillow, one eye open. I read the result, read it again, and seriously considered that the language was making fun of me personally.
This post takes that single line apart, one tiny piece at a time, until it stops being magic and starts being almost boring. That is a promise: boring is what we are aiming for.
The line that broke my brain
console.log([] == ![]); // true
An empty array equals the opposite of an empty array. Sure. Of course it does. π
First, what does ![] even mean?
console.log(![]); // false
The ! flips a value to its opposite. Arrays count as "truthy" in JavaScript β even an empty one β so flipping an array gives you false.
So the scary line is really asking something simpler: is [] equal to false?
Round two: [] against false
console.log([] == false); // true
Still true, but now the fight is fair and we can watch it happen slowly. The == operator, when the two sides are different types, quietly converts things before comparing. It has rules. Weird rules, but rules.
false puts on a number costume
console.log(Number(false)); // 0
When a boolean meets ==, it gets converted to a number first. false becomes 0. Hold onto that.
And [] is secretly a string
console.log(String([])); // ""
An array, asked to become a string, joins its items with commas. An empty array has no items, so it becomes an empty string β nothing between the quotes.
And that string is secretly a number
console.log(Number("")); // 0
The other side of our comparison is a number now (remember, false turned into 0), so the string gets converted too. An empty string becomes 0.
So the whole mystery is justβ¦
console.log(0 == 0); // true
That is it. [] travelled down the road array β string β number and arrived at 0. false arrived at 0 by the front door. 0 == 0 is true, and the "magic" was three boring conversions standing on each other's shoulders.
The version that behaves: ===
console.log([] === ![]); // false
Triple equals does no converting at all. Different types, instant false, conversation over. This is why people tell beginners to use ===. Not because == is evil, but because == does secret extra work.
Now you try one (predict it first)
console.log("0" == false); // true
Cover the result with your thumb and guess before you look. A string against a boolean feels like it should fail instantly, right?
Run the rulebook instead. The boolean converts first: false becomes 0. Then the string gets its turn, and "0" becomes the number 0. Two zeros, one true, no mystery left. If you predicted it, congratulations: you are no longer memorising JavaScript weirdness, you are operating it. That shift is the entire game at this level.
One last trap, free of charge
console.log([] == []); // false
Two empty arrays are not equal either, because each [] you type builds a brand-new array, and objects compare by identity, not by contents. JavaScript did not forget how equality works. It just has a very specific, very literal idea of it.
What I got wrong for weeks
I treated results like this as proof the language was random, so I memorised answers instead of learning rules. The honest truth: I also once spent 20 minutes "fixing" code that was not broken, because the weird result had convinced me something upstream was wrong.
Nothing was wrong. I just did not know the conversion rules existed.
If you are a beginner and a line like this made you feel stupid, hear it from someone who felt exactly that at 1 a.m.: the language is not mocking you. It is following a rulebook nobody handed you yet. Feeling lost in front of a rulebook you have never seen is not the same as being bad at this.
Next in this little series, I am taking apart {} + [], which might be even ruder. Before I do β what do you think it gives: a number, a string, or an error?
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes β full credit and traffic to the original publisher.