Javascript typing (IntString) question - Stack Overflow

I have encountered something weird (probably not, it's more likely that I don't really get it

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 / the alerts will go "2", "3", "4"

When I reverse the order and do this (/):

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!

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/5mdmJ/ the alerts will go "2", "3", "4"

When I reverse the order and do this (http://jsfiddle/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!

Share asked Aug 29, 2011 at 12:04 m90m90 11.8k15 gold badges67 silver badges119 bronze badges
Add a ment  | 

7 Answers 7

Reset to default 3

The single plus operator is overloaded for strings and ints.In the first example, the ++ operator is only defined for ints, 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 parsion 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..

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744856273a4597425.html

相关推荐

  • Javascript typing (IntString) question - Stack Overflow

    I have encountered something weird (probably not, it's more likely that I don't really get it

    2天前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信