What does the following code output? Why?
Console.log (1 + "2" + "2"), Console.log (1 + + "2" + "2"), Console.log (1 +-"1" + "2"), Console.log (+ "1" + "1" + "2"); CONSOLE.L OG ("A"-"B" + "2"); Console.log ("A"-"B" + 2);
The results were
122,32,02,112,nan2,nan
The second and third questions are to be noted here.
The second question of the + "2" result is to convert the string 2 into the number 2, the result is a string 32
The third question-the result of "2" is to convert the string 2 into the number 2, resulting in a string of 02
The +,-operator acts as a transform here, converting a string to a computable expression, which is useful for writing self-executing functions.
(function () {}) () //Return Undefined+function () {} () //Return Nan-function () {} () //Return Nan!function () {} () //Return true~ function () {} () //Return-1
These can act as self-executing, but the return value is different.
JavaScript operators apply