JavaScript – Why Does !{}[true] Evaluate to True

javascript

{}[true] is [true] and ![true] should be false.

So why does !{}[true] evaluate to true?

Best Answer

I believe that's because plain {}[true] is parsed as an empty statement block (not an object literal) followed by an array containing true, which is true.

On the other hand, applying the ! operator makes the parser interpret {} as an object literal, so the following {}[true] becomes a member access that returns undefined, and !{}[true] is indeed true (as !undefined is true).