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

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

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

9条回答
  •  旧时难觅i
    2020-11-22 07:39

    1. Unary plus given string converts to number
    2. Increment operator given string converts and increments by 1
    3. [] == ''. Empty String
    4. +'' or +[] evaluates 0.

      ++[[]][+[]]+[+[]] = 10 
      ++[''][0] + [0] : First part is gives zeroth element of the array which is empty string 
      1+0 
      10
      

提交回复
热议问题