Why is an empty array equal to false?

Viewed 315

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 Answers

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.

Related