I'm puzzled with the type convert in javascript .
var temp = "111"
temp = temp + 0 // temp is "1110" now
temp = temp - 0 // temp is number , 111
temp = "1110" - 0 // temp is number 1110
I'm curious what causes such differences in two ways .
Sorry , I forgot the assignment led to abnormal results .
-
This is because string concatenation in javascript is done with the plus sign, and of course you can't subtract a string from a string, so there is no equivalent for the minus sign, so when you think about it, it makes perfect sense.adeneo– adeneo2014年08月08日 06:30:44 +00:00Commented Aug 8, 2014 at 6:30
-
temp = temp - 0 // temp is number , 111 is not correct. you will get 1110jforjs– jforjs2014年08月08日 06:32:58 +00:00Commented Aug 8, 2014 at 6:32
-
1This question appears to be off-topic because it seems to be based on an observation error.Felix Kling– Felix Kling2014年08月08日 06:40:58 +00:00Commented Aug 8, 2014 at 6:40
3 Answers 3
I doubt that
temp = temp - 0 // temp is number , 111
yields the result you mentioned there. See http://jsbin.com/faquvobo/1/edit?js,output
You have an observation error (maybe you actually used temp = 0 + temp;).
General explanation:
The + operator is overloaded and the - operator isn't.
If you use the + operator and one operand is a string, the operand is converted to a string and string concatenation is performed (instead of addition).
The - operator is only defined for numbers, so both operands are converted to numbers first and subtraction is performed.
1 Comment
in javascript, the + operator performs typecasting to string if either of the operands is not a number, while the - operator always casts to number.
so your code will look like this after typecasting:
var temp = "111";
//before cast and variable evaluation
var temp2 = temp + 0;
//after cast and variable evaluation
var temp2 = "111" + "0"; // evaluates to string "1110" => string concatenate
//temp2 is string "1110"
//before cast and variable evaluation
temp2 = temp2 - 0;
//after cast and variable evaluation
temp2 = 1110 - 0; //evaluates to number 1110 => number subtraction
//temp2 is number 1110
//before cast and variable evaluation
var temp3 = "1110" - 0;
//after cast and variable evaluation
var temp3 = 1110 - 0; // evaluates to number 1110 => number subtraction
//temp3 is number 1110
Comments
From ECMAScript Language Specification. For Addition operator:
7.If Type(lprim) is String or Type(rprim) is String, then Return the String that is the result of concatenating ToString(lprim) followed by ToString(rprim)
8.Return the result of applying the addition operation to ToNumber(lprim) and ToNumber(rprim).
This means + operator will try String firstly if one of them is String. Otherwise, it will apply number addition by converting them into number.
For Subtraction Operator :
5.Let lnum be ToNumber(lval).
6.Let rnum be ToNumber(rval).
7.Return the result of applying the subtraction operation to lnum and rnum
It means - operator will always convert to number and apply number subtraction operation.