Difference between == and === in JavaScript
Awkward formatting:
a =+ b;
is equivalent to:
a = +b;
And +b
is just a fancy way of casting b
to number, like here:
var str = "123";
var num = +str;
You probably wanted:
a += b;
being equivalent to:
a = a + b;
The + sign before the function, actually called Unary plus and is part of a group called a Unary Operators and (the Unary Plus) is used to convert string and other representations to numbers (integers or floats).
A unary operation is an operation with only one operand, i.e. a single input. This is in contrast to binary operations, which use two operands.
Basic Uses:
const x = "1";
const y = "-1";
const n = "7.77";
console.log(+x);
// expected output: 1
console.log(+n);
// expected output: 7.77
console.log(+y);
// expected output: -1
console.log(+'');
// expected output: 0
console.log(+true);
// expected output: 1
console.log(+false);
// expected output: 0
console.log(+'hello');
// expected output: NaN
When the + sign is positioned before a variable, function or any returned string representations the output will be converted to integer or float; the unary operator (+) converts as well the non-string values true, false, and null.