Why does ++[[]][+[]]+[+[]] return the string “10”?

后端 未结 9 2172
傲寒
傲寒 2020-11-22 06:50

This is valid and returns the string \"10\" in JavaScript (more examples here):

9条回答
  •  温柔的废话
    2020-11-22 07:22

    Step by steps of that, + turn value to a number and if you add to an empty array +[]...as it's empty and is equal to 0, it will

    So from there, now look into your code, it's ++[[]][+[]]+[+[]]...

    And there is plus between them ++[[]][+[]] + [+[]]

    So these [+[]] will return [0] as they have an empty array which gets converted to 0 inside the other array...

    So as imagine, the first value is a 2-dimensional array with one array inside... so [[]][+[]] will be equal to [[]][0] which will return []...

    And at the end ++ convert it and increase it to 1...

    So you can imagine, 1 + "0" will be "10"...

提交回复
热议问题