I know first execute +[]
, and convert it to 0
so the expression become
[[][[]]+[]][0][++[0][0]]
then I think the ++
have high priority, but the ++[0]
seem is illegal
So why the final result is n
?
I know first execute +[]
, and convert it to 0
so the expression become
[[][[]]+[]][0][++[0][0]]
then I think the ++
have high priority, but the ++[0]
seem is illegal
So why the final result is n
?
Pavlovian response, I just had to work it out. :-)
Start from the inside out:
[ [][[]]+[] ][0][ ++[0][0] ]
Let's take [][[]]+[]
first: []
is an empty array. The [[]]
after it is a property accessor. So the inner []
there is coerced to the string ""
. [][""]
is undefined
(there is no property on the array with the name ""
.) Taking undefined + []
coerces both sides to string and gives us "undefined"
. So now we have:
[ "undefined" ][0][ ++[0][0] ]
The next part is fairly obvious: ["undefined"][0]
is "undefined
". So now we have:
"undefined"[ ++[0][0] ]
Let's do that property accessor, ++[0][0]
: [0]
is an array containing 0. [0]
after it indexes into it, so we're incrementing the array entry at index 0 (which is 0) to make it 1.
So now we have:
"undefined"[1]
...which is "n".