Why is an empty array equal to false?

I was reading the “Types and Grammar” section of the You Don’t Know JS book and I am having a hard time understanding this.

Why is an empty array equal to false?

console.log(false == []); // true

1 answer

  • answered 2020-06-27 04:06 Pain

    When you do [] == false, behind the scenes the Array.prototype.toString method is called with that empty array as its this context, which returns the empty string "" and the empty string is a falsy value in JavaScript.