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

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

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

9条回答
  •  孤城傲影
    2020-11-22 07:17

    Let’s make it simple:

    ++[[]][+[]]+[+[]] = "10"
    
    var a = [[]][+[]];
    var b = [+[]];
    
    // so a == [] and b == [0]
    
    ++a;
    
    // then a == 1 and b is still that array [0]
    // when you sum the var a and an array, it will sum b as a string just like that:
    
    1 + "0" = "10"
    

提交回复
热议问题