-3

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 ?

hh54188
  • 14,887
  • 32
  • 113
  • 184
  • See https://stackoverflow.com/questions/9032856/what-is-the-explanation-for-these-bizarre-javascript-behaviours-mentioned-in-the – T.J. Crowder Feb 23 '18 at 10:35

1 Answers1

2

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".

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875