Why does this JavaScript statement:
console.log(1 + + \"2\");
3
as the output? I am not sure why
+
or -
operand in front of a string converts it to number. so here +"2"
will become 2
hence the result will be 3
.
=> 1 + + "2" // +"2" = 2
=> 1 + 2
=> 3
If you use -
in between like
=> 1 - - "2" // -"2" = -2
=> 1 - - 2 // 1 - (-2)
=> 1 + 2
=> 3
So,
-"2" ==> -2
+"2" ==> 2
+"Hello" ==> NaN
-"Hello" ==> NaN