Why [] == ![] is True in JavaScript – The Weirdest Comparison Explained! Here's Why JavaScript Says It's True and JavaScript Logic Uncovered

Why sqare bracket equal equal not sqare bracket is True in JavaScript The Weirdest Comparison Explained Here is Why JavaScript Says It is True and JavaScript Logic Uncovered

Our code :-

console.log([] == ![]);

Step 1: Evaluate Right-Hand Side – ![]

  • ![] means: logical NOT of an array

But wait — is an array [] truthy or falsy?

In JavaScript, all objects are truthy, including empty arrays.

So:

![] → !truthy → false

👉 So now the expression becomes:

[] == false

Step 2: Coerce Both Sides – [] == false

Now we’re comparing:

  • [] → object
  • false → boolean

Rule:

When comparing an object with a primitive using ==, JavaScript:

  • Tries to convert the object to a primitive (usually a string or number)

So:

[] → "" // empty array becomes empty string via `.toString()`

Now it becomes:

"" == false

Step 3: Compare "" == false

Now both sides are primitives, but different types:

  • "" is a string
  • false is a boolean

Again, JavaScript tries to coerce them to numbers:

""0
false0

So it becomes:

0 == 0true

✔ Final Answer:

A) true

This question appeared in the TCS 2nd round of interview, testing the candidate’s knowledge of comparisons operator between arrays with inversion operator  in JavaScript.

Post a Comment

Previous Post Next Post