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

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

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

9条回答
  •  深忆病人
    2020-11-22 07:38

    This one evaluates to the same but a bit smaller

    +!![]+''+(+[])
    
    • [] - is an array is converted that is converted to 0 when you add or subtract from it, so hence +[] = 0
    • ![] - evaluates to false, so hence !![] evaluates to true
    • +!![] - converts the true to a numeric value that evaluates to true, so in this case 1
    • +'' - appends an empty string to the expression causing the number to be converted to string
    • +[] - evaluates to 0

    so is evaluates to

    +(true) + '' + (0)
    1 + '' + 0
    "10"
    

    So now you got that, try this one:

    _=$=+[],++_+''+$
    

提交回复
热议问题