Join waitlist
JavaScriptCommunity question

why is [] == ![] true in js??

Asked by Priya Sharma 2 months ago 3 answers 14 votes

got this in a battle and i was 100% sure its false. empty array equal to not of itself?? how

can someone explain step by step pls

3 answers

Arjun Mehta2 months agoMost voted11

![] gets evaluated first. arrays are truthy (even empty ones) so ![] = false.

now you have [] == false. with == both sides get converted to numbers:
false -> 0
[] -> "" -> 0

0 == 0 so true.

this is why everyone uses === , it doesn't do any of this conversion

aman khan2 months ago4

and [] == [] is false btw because objects are compared by reference. js is fun

Priya Sharma2 months ago1

ohh the ! runs first. i was comparing [] with [] in my head. thanks

Related questions

Go deeper

Want the full explanation, not just an answer?

Structured lessons on DSA, core CS, Java and interview skills, each ending in a quiz so you know it stuck.