12
var a = "ab"; var b = "ab"; a+=1; // "ab1" b++; // "NaN" 

(Tested on chrome's V8)

Can someone explain why the results are different based on the internal atomic actions of the ++ arithmetic operator and the += assignment operator with argument 1

1
  • 2
    Try a = '12';, and try ++, --, +=1 and -=1. JavaScript is fun. Commented Dec 9, 2010 at 12:02

3 Answers 3

10

++ converts to number, and then increments, += with a String concatenates.

From the spec:

11.3.1 Postfix Increment Operator

  ...
  3. Let oldValue be ToNumber(GetValue(lhs)).
  4. Let newValue be the result of adding the value 1 to oldValue, using the same rules as for the + operator (see 11.6.3).

For the a+=1 case, if you add a number to a string or the other way around the number gets converted to a string:

11.6.1 The Addition operator ( + )

  ...
  7. If Type(lprim) is String or Type(rprim) is String, then
      a. 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).

Sign up to request clarification or add additional context in comments.

1 Comment

This only covers half the question, you should also note that in the +=1 case 1 is converted to a string for the concatenation.
10
  • ++ tries to increment a Number (if it's not a number, this will fail - resulting in NaN)
  • += is concatenation, in this case the JavaScript engine figures out that one side is a string, so they're both concatenated as strings.

They're different because they're different operations, ++ is specifically an arithmetic operator, where as += is a more general assignment operator that behaves differently based on the data type - specifically, string has its own implementation.

7 Comments

You would think ++ & +=1 are logically equivelant. I guess I forgot + is overloaded for String & Number
@Raynos - yup, well string overloads + and += anyway, but it's more that ++ is specific to number here :)
@nick: I am really curious about you, cause i really see you answering the questions 24/7 which is really good :D to help people wish i could do this, But i really want to know do you sleep !? and do you have a job ? :D
@Raynos, @Nick Craver: I think the equivalent to x += 1 is ++x, not x++.
@Omeid - Check my activity log, I was off after around 7am yesterday morning ;) It just seems otherwise since you're likely on the same times as I am :)
|
4

That's because the + operator in javascript is both the mathematical + and the string concatenation operator, while the ++ is always a mathematical operator.

So, when you have:

string = string + number;

the number is converted to string and concatenated to the first string.

When you have

string++

you will convert the string to a number, getting NaN, and then add one to that - getting still, NaN.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.