12

Possible Duplicate:
Can you explain why ++[[]][+[]]+[+[]] = 10

I'm wondering something for few days...I know that unary plus in JavaScript first converts it's operand to Number. I'm applying + to an empty array and I get the following result:

+[] == 0

When I do this:

+[1] == 1

But:

+[1,2] == NaN

The last two things are almost clear but why the empty array is 0?! Is this connected with:

[] == false

Some times ECMAScript makes me wonder a lot...

alert([![]+[]][+[]][+[]]+[![]+[]][+[]][+!+[]]+[!+[]+[]][+![]][+![]]+[![]+[]][+[]][+!+[]]+[![]+[]][+[]][+!+[]+!+[]]+' '+(![]+[])[+[]]+(![]+[])[+!+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]);

Best regards!

Community
  • 1
  • 1
  • 7
    http://stackoverflow.com/questions/7202157/can-you-explain-why-10/7202287#7202287 at the end I posted an explanation for `+[] === 0`. – pimvdb Nov 19 '11 at 16:58

1 Answers1

7

The stringified form of the empty Array is an empty string:

> [].toString()
""

The unary operator + converts to Number objects, so, it converts an empty string to 0:

> Number("")
0

This explains why +[] == 0 is true.

dossy
  • 1,617
  • 16
  • 26