The Strangest Equality in JavaScript — Explained with Real Examples

Aryan JogiAryan Jogi
3 min read

🧠 Why [] == ![] is true and other JavaScript WTFs 🤯

If you've ever stared at a JavaScript comparison and thought,

"This can't be right..."
You're not alone.

JavaScript’s type coercion is infamous for producing results that seem straight-up wrong — until you understand what’s really going on under the hood.

Let’s walk through some of the quirkiest examples — and break them down step by step.


🧪 Example 1: [] == ![] // true

Wait, what?!
An empty array is equal to its own negation?

Let’s break it down:

[] == ![]

Step-by-step:

  1. ![] → First, ![] is evaluated.

    • An empty array is truthy.

    • So ![] becomes false.

  2. Now we're comparing: [] == false

  3. JavaScript converts the array to a primitive.

    • []'' (empty string)
  4. Then it converts both sides to numbers:

     '' == false
     0 == 0 // true
    

👉 Final result: true

Wild, right?


🧪 Example 2: [] == [] // false

This one feels even more confusing:

[] == [] // false

Why false?

Because JavaScript compares references when using == (or ===) for objects and arrays.

const a = [];
const b = [];

a == b // false — different memory locations

👉 Even if the contents are the same, they’re different objects.


🧪 Example 3: {} + [] → 0

{} + [] // 0

This looks like it should concatenate an object and an array, right?

But here's the trick:
When this runs in the browser (outside a block), the {} is interpreted as an empty block, not an object.

So it becomes:

+[] // 0

The unary + converts [] to a number:

Number([]) → 0

👉 Final result: 0


🧪 Example 4: [] + {}"[object Object]"

Here’s one that actually is string concatenation:

[] + {} // "[object Object]"

How?

  1. [] becomes '' (empty string)

  2. {} becomes '[object Object]'

  3. Concatenation gives:

     '' + '[object Object]''[object Object]'
    

🎯 TL;DR — What to Remember

  • == does type coercion — things are often converted to numbers or strings before comparison.

  • Arrays/objects are compared by reference, not value.

  • + can mean numeric addition or string concatenation depending on context.

  • Always use === unless you really know what you're doing.


💡 Pro Tip:

If a comparison feels weird, try breaking it down like the engine does.
Or better yet — avoid == altogether unless you need type coercion magic.


💬 Bonus

What’s the weirdest equality case you’ve seen in JavaScript?

Drop your favorite WTF JS comparison in the comments 👇


0
Subscribe to my newsletter

Read articles from Aryan Jogi directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Aryan Jogi
Aryan Jogi