Menu

Yes, [] == ![] Is True. No, JavaScript Isn't Broken (I Checked, Slowly).
πŸ“°

Yes, [] == ![] Is True. No, JavaScript Isn't Broken (I Checked, Slowly).

DEV Community: beginnersΒ·AheadKitΒ·8 days ago
#TeAemx6b
#devtobeginners#devto#dev#false#string
Reading 0:00
15s threshold

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

Enter fullscreen mode

Exit fullscreen mode

An empty array equals the opposite of an empty array. Sure. Of course it does. πŸ™‚

First, what does ![] even mean?

console . log ( ! []); // false

Enter fullscreen mode

Exit fullscreen mode

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 .…

Continue reading β€” create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.