Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto-type conversion in JavaScript

Tags:

javascript

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?

like image 633
Tiny Avatar asked Oct 19 '12 20:10

Tiny


4 Answers

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.

like image 58
Simon Boudrias Avatar answered Oct 22 '22 09:10

Simon Boudrias


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..."

like image 30
edward ingram Avatar answered Oct 22 '22 10:10

edward ingram


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.

like image 32
aecolley Avatar answered Oct 22 '22 08:10

aecolley


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).

like image 31
Boris D. Teoharov Avatar answered Oct 22 '22 10:10

Boris D. Teoharov