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
  • 18,263
  • 7
  • 55
  • 75
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
  • 76,500
  • 11
  • 62
  • 80
Marik Ishtar
  • 2,899
  • 1
  • 13
  • 27