The following all expressions in JavaScript are far obvious.
var x = 10 + 10;
The value of x
is 20
.
x = 10 + '10';
The value of x
in this case is 1010
because the +
operator is overloaded. If any of the operands is of type string, string concatenation is made and if all the operands are numbers, addition is performed.
x = 10 - 10;
x = 10 - '10';
In both of these cases, the value of x
will be 0
because the -
operator is not overloaded in that way and all operands are converted to numbers, if they are not before the actual subtraction is performed (you may clarify, if anyway I'm wrong).
What happens in the following expression.
x = '100' - -'150';
The value of x
is 250
. Which also appears to be obvious but this expression somewhat appears to be the equivalent to the following expression.
x = '100' +'150';
If it had been the case then these two strings would have been concatenated and assigned 100150
to x
. So why is addition performed in this case?
EDIT :
+'10' + 5
returns 15
and 'a' + + 'b'
returns aNaN
. Does anyone know why?
In your case - -
is not evaluated first to become equivalent to +
. -"150"
is evaluated as a number, and so became -150
.
As you can't subtract a string (NaN
), JS then take "100"
and make a number, and then it run 100
- -150
which is 250.
The key is really that you can't subtract type string, so it converts those strings to numbers.
The + and - operators respond differently to strings.
The + operator concatenates strings; however, the - operator does not do the reverse (split strings).
So if JavaScript sees '100' +'150'
, it thinks "Hey, I see strings with a +... I can concatenate them."
If JS sees '100' - -'150'
, it thinks, "Hey, I see strings with a - .. i can't really do any string functions, so I'll treat them as numbers..."
The unary -
operator always converts its operand to Number (ECMA-262 s. 11.4.6). So
x = '100' - -'150';
is equivalent to
x = '100' - -150;
which reduces further to
x = 100 - -150;
because the binary -
operator also always converts its operands to Number (s. 11.6.2).
By contrast, the unary +
operator converts its operands to strings if either one is already a string (s. 11.6.1).
You can find the complete spec for ECMAscript (and therefore for the core of Javascript) at http://www.ecma-international.org/publications/standards/Ecma-262.htm.
If JS sees minus operator used on a string it firstly tries to type cast it to number and then evaluates expression because Minus operator is used only for arithmetical operations. Plus operator could mean in first place concatenation and then addition.
In some other weakly typed languages like PHP this ambiguity is eliminated by using two different operators for concatenation and addition.
However the proper way to use arithmetic on strings is to type cast them manually to numbers (using parseInt).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With