what it will print console.log(1+ + “2”)

前端 未结 3 1393
名媛妹妹
名媛妹妹 2021-01-16 22:53

Why does this JavaScript statement:

console.log(1 +  + \"2\");

print

3

as the output? I am not sure why

3条回答
  •  臣服心动
    2021-01-16 23:18

    Regarding the specific output of

    console.log(1 +  + "2");
    

    Run it on your browser console. The better question is why does it output what it does -

    console.log(1 +  + "2");
                  ^
    

    That is the binary + operator, which will concatenate strings or add numbers.

    console.log(1 +  + "2");
                     ^
    

    That one is the unary + operator, which converts "2" to a number.

    Don't create JavaScript like this. It's confusing.

提交回复
热议问题