Javascript typing (Int/String) question?

The answer is that since js is loosely typed it starts with the first operation that youre performing.

Up vote 2 down vote favorite share g+ share fb share tw.

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 jsfiddle.net/5mdmJ/ the alerts will go "2", "3", "4" When I reverse the order and do this (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!

Javascript typing link|improve this question asked Aug 29 '11 at 12:04m902,0621216 100% accept rate.

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.

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.

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

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions