57

I'm a bit confused with the += sign. How does it work?

  1. 1 += 2 // equals ?

  2. and this

    var data = [1,2,3,4,5];
    var sum = 0;
    data.forEach(function(value) {
        sum += value; 
    });
    sum = ?
    
Braiam
  • 4,345
  • 11
  • 47
  • 69
muudless
  • 6,744
  • 7
  • 34
  • 56
  • 1
    After `1 += 2` 1 is equal 3. Not all that much of a problem. But `1 -= 1;` makes boolean expressions a pain in the neck! – SF. Jul 26 '11 at 07:05
  • 4
    Stating the obvious -- you could try this in the JavaScript console of your browser: after the forEach, sum is 15. – Sam Dutton Jul 26 '11 at 15:03
  • Didn't know you could do this inline using the value. – Zach Jan 01 '21 at 02:08

14 Answers14

82

1 += 2 is a syntax error (left-side must be a variable).

x += y is shorthand for x = x + y.

sandstrom
  • 12,776
  • 5
  • 59
  • 60
lawnsea
  • 6,242
  • 1
  • 22
  • 17
  • This is buggy as hell in javascript tho, according to javascript: 198.51 -= 84.4 results in 114.10999999999999 Which is incorrect. – Jean-Paul Jun 15 '17 at 13:26
  • 25
    @JpHouten Your comment isn't really on this answer, but even still I felt I should reply to it. First, you mean -, not -= since you 198.51 -= 84.4 just gives an error. But mostly, this isn't a bug. This is just a consequence of the fact that computers represent numbers in binary instead of decimal and numbers like 198.51 and 84.4 can't be represented exactly in binary, but have to be rounded. This is a standard part of floating point numbers. If you need exact (rather than approximate) decimal calculations, you should use fixed point numbers instead. – Keith Irwin Jul 05 '17 at 08:45
  • Actually x+=y equals to x = x + (y). Look at this https://stackoverflow.com/a/65325247/2913723 – sabbir.alam Dec 16 '20 at 16:17
32

1) 1 += 2 // equals ?

That is syntactically invalid. The left side must be a variable. For example.

var mynum = 1;
mynum += 2;
// now mynum is 3.

mynum += 2; is just a short form for mynum = mynum + 2;

2)

var data = [1,2,3,4,5];
var sum = 0;
data.forEach(function(value) {
    sum += value; 
});

Sum is now 15. Unrolling the forEach we have:

var sum = 0;
sum += 1; // sum is 1
sum += 2; // sum is 3
sum += 3; // sum is 6
sum += 4; // sum is 10
sum += 5; // sum is 15
Paul
  • 130,653
  • 24
  • 259
  • 248
17

That is just a short form for:

sum = sum + value;
Tudor Constantin
  • 24,065
  • 7
  • 44
  • 66
13

+= in JavaScript (as well as in many other languages) adds the right hand side to the variable on the left hand side, storing the result in that variable. Your example of 1 +=2 therefore does not make sense. Here is an example:

var x = 5;
x += 4; // x now equals 9, same as writing x = x + 4;
x -= 3; // x now equals 6, same as writing x = x - 3;
x *= 2; // x now equals 12, same as writing x = x * 2;
x /= 3; // x now equals 4, same as writing x = x / 3;

In your specific example the loop is summing the numbers in the array data.

Paul
  • 18,243
  • 13
  • 70
  • 92
5

+= operator is used to concatenate strings or add numbers.

It will increment your sum variable with the amount next to it.

var sum = 0;
var valueAdded = 5; 

sum += valueAdded;

sum = 5

Adriano Carneiro
  • 53,285
  • 12
  • 85
  • 120
cillierscharl
  • 6,595
  • 3
  • 26
  • 45
4

You have to know that:

  • Assignment operators syntax is: variable = expression;

    For this reason 1 += 2 -> 1 = 1 + 2 is not a valid syntax as the left operand isn't a variable. The error in this case is ReferenceError: invalid assignment left-hand side.

  • x += y is the short form for x = x + y, where x is the variable and x + y the expression.

    The result of the sum is 15.

      sum = 0;
      sum = sum + 1; // 1
      sum = sum + 2; // 3
      sum = sum + 3; // 6
      sum = sum + 4; // 10
      sum = sum + 5; // 15

Other assignment operator shortcuts works the same way (relatively to the standard operations they refer to). .

Paolo
  • 16,171
  • 20
  • 78
  • 110
3

...and don't forget what happens when you mix types:

x = 127;
x += " hours "
// x is now a string: "127 hours "
x += 1 === 0;
// x is still a string: "127 hours false"
Sam Dutton
  • 13,238
  • 6
  • 50
  • 61
1

that's just a shorthand notation in most languages.which means that

x=x+1;

we can do the same operation for x-=1,x*=1,x/=1; which means

> x=x-1; x=x*1; x=x/1;

ManiShankar
  • 35
  • 2
  • 8
1

a += b is shorthand for a = a +b which means:

1) 1 += 2 // won't compile

2) 15

Timothy Jones
  • 19,847
  • 5
  • 54
  • 87
0

As everyone said above

var str = "foo"
str += " bar"
console.log(str) //will now give you "foo bar"

Check this out as well https://www.sitepoint.com/shorthand-javascript-techniques/

OluO
  • 74
  • 3
0

x += 1 is just shorthand for x = x + 1 It can also be used for strings:

var string = "foo"
string += "bar"
0

x+=y is shorthand in many languages for set x to x + y. The sum will be, as hinted by its name, the sum of the numbers in data.

-1
  1. 1 += 2 won't throw an error but you still shouldn't do it. In this statement you are basically saying "set 1 equal to 1 + 2" but 1 is a constant number and not a variable of type :number or :string so it probably wouldn't do anything. Saying
    var myVariable = 1
    myVariable += 2
    console.log(myVariable)
    
    would log 3 to the console, as x += y is just short for x = x + y
  2. var data = [1,2,3,4,5]
    var sum
    data.forEach(function(value){
      sum += value
    })
    
    would make sum = 15 because:
    sum += 1 //sum = 1
    sum += 2 //sum = 3
    sum += 3 //sum = 6
    sum += 4 //sum = 10
    sum += 5 //sum = 15
    
-1

NO 1+=2!=2 it means you are going to add 1+2. But this will give you a syntax error. Assume if a variable is int type int a=1; then a+=2; means a=1+2; and increase the value of a from 1 to 3.

bogatyrjov
  • 5,077
  • 9
  • 29
  • 60
avirk
  • 2,912
  • 7
  • 34
  • 56