JavaScript array to number

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!


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.

链接地址: http://www.djcxy.com/p/48044.html

上一篇: 解释为什么'+ [] == 0'在Javascript中输出'true'?

下一篇: JavaScript数组编号