I have encountered something weird (probably not, it's more likely that I don't really get it) in JavaScript and I'd be curious to find out why things behave like they do.
When I do:
var index = '1';
index++;
alert(index);
index = index + 1;
alert(index);
index = true ? index + 1 : 0;
alert(index);
as in http://jsfiddle.net/5mdmJ/ the alerts will go "2", "3", "4"
When I reverse the order and do this (http://jsfiddle.net/5mdmJ/1/):
var index = '1';
index = true ? index + 开发者_如何学编程1 : 0;
alert(index);
index = index + 1;
alert(index);
index++;
alert(index);
I'll have "11", "111" and "112".
I do know that this is something with index being a string, but I don't really get why it's int-typed all the way through in example 1 and string-typed in exampled two. I know this is probably going to be really simple but I could not find anything by now that really clarifies to me the logic behind what's going on. Does the type change? Why and when does this happen?
Thanks for any hint or article or whatever!
The single plus operator is overloaded for strings and ints.In the first example, the ++
operator is only defined for int
s, so index gets converted to a number, then incremented. Afterwards, the plus operator indicates addition. Since index is a string in the second example, the plus operator indicates concatenation.
but I don't really get why it's int-typed all the way through in example 1
Unlike +
, which has two meanings (addition for number, concatenation for strings), ++
has no ambiguity - it always means "increment"
So when you run ++
on a string, it converts it into a number. Since this doesn't happen in example #2, the +
operations are all concatenations.
var x = '1';
x++;
alert(typeof x); // "number"
The answer is that since js is loosely typed it starts with the first operation that youre performing.
In your example 1 the first operation is an exclusive arithmetic operation and js correctly interprets it and considers it INT all the way
In your example 2 the first operation is an comparsion operation and js interprets it as boolean and then its immediately close property string.
Thats why you get different behaviours.
This: index++;
is an number function. Notice that I didn't say integer. There is no such thing as an integer in JavaScript. All numbers are floating point numbers.
This: index = true ? index + 1 : 0;
is string concatenation, because index is a string. If index is a number then it would add the two together.
So what is happening is that the ++ operator is converting the string to a number and adding the values. In the second its converting the number to a string and appending the two strings together to form a new string (remember in JavaScript strings are immutable).
The reason is that variable++ will convert the variable first to number and then increase it by one. Whilst variable + 1 will only add 1 to the variable, but not converting it.
It's because of the type priority when you concatenate strings or variables.
"1" + 2 + 3; // -> "123"
4 + 3 + "2"; // -> "72" ; 4 + 3 = 7 ; 7 + "2" = "72"
My GUESS is, that when you do index++
, it's considered an "int", and stays that way through example one, but when you do index + 1
, it's considered a string, and stays like that through example 2..
精彩评论