-3

I just saw a js question which is:

What's the result of:

if ([]) {console.log(1);}  
if ([].length) {console.log(2);}  
if ({} === {}) {console.log(3);}  
if ('' == 0) {console.log(4);}

I tried with the console and it gave

1
4

Why [].length gives false? Is the length undefined?

xzifan
  • 27
  • 5

1 Answers1

3

The length of an empty Array is 0, which is a "falsy" value in JavaScript.

See MDN for further reference:

m90
  • 11,434
  • 13
  • 62
  • 112