Often, the sign +
is placed before
the string instead of Number
to
shorten the code. Thus, an operation is
performed on the string that is valid
only for numbers, and the string is
converted to a number.
See an example:
let a = +'2'; // the number 2 will be written to the variable
let b = +'3'; // the number 3 will be written to the variable
alert(a + b); // shows 5
Here is another example:
let a = '2';
let b = +a; // the number 2 will be written to b
And here is the following example,
although working, but it does not
look very nice. It would be more
appropriate to use the function Number
:
let a = '2';
let b = '3';
alert(+a + +b); // shows 5
Given a code:
let a = '2';
let b = '3';
alert(a + b); // shows '23'
Using the described above technique
with a plus, correct the above code
such a way that the variables a
and b
are assigned a number,
not a string, and the result, respectively,
would be not '23'
, but 5
.