[javascript] How does += (plus equal) work?

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 = ?
    

This question is related to javascript

The answer is


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.


To be precise a+=b not actually equals to a = a + b. It actually is a = a + (b). How so? Let me show you a demo,

_x000D_
_x000D_
a = 1;
console.log('a += 1<<2: ', a += 1<<2); // results in 5

a = 1;
// If a += b is equal to a = a + b then this would be 5. But as you see this is not. The result is 8.
console.log('a + 1 << 2: ', a + 1 << 2); // results in 8


a = 1;
// As you can see this results in 5.
console.log('a + (1<<2): ', a + (1<<2)); // results in 5
_x000D_
_x000D_
_x000D_

Because this += or *= or -= or /= etc operators implicitly groups the right hand side.


That is just a short form for:

sum = sum + value;

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

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

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


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

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

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


As everyone said above

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

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


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.


  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
    

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

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

2) 15


+= 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


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

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;


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