3

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
Sebastian Simon
  • 16,564
  • 7
  • 51
  • 69
Lieutenant
  • 39
  • 2

1 Answers1

3

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.

Unmitigated
  • 46,070
  • 7
  • 44
  • 60
Marik Ishtar
  • 2,653
  • 1
  • 10
  • 24