muudless
muudless

Reputation: 7532

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

Upvotes: 63

Views: 163973

Answers (13)

Jonah C Rowlinson
Jonah C Rowlinson

Reputation: 485

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

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

Upvotes: -1

OluO
OluO

Reputation: 94

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/

Upvotes: 2

ManiShankar
ManiShankar

Reputation: 35

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;

Upvotes: 1

lawnsea
lawnsea

Reputation: 6581

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

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

Upvotes: 91

cillierscharl
cillierscharl

Reputation: 7117

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

Upvotes: 5

Sam Dutton
Sam Dutton

Reputation: 15269

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

Upvotes: 3

avirk
avirk

Reputation: 3086

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.

Upvotes: -1

Paolo
Paolo

Reputation: 21056

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

Upvotes: 4

Paul
Paul

Reputation: 141839

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

Upvotes: 33

Paul
Paul

Reputation: 20061

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

Upvotes: 13

Timothy Jones
Timothy Jones

Reputation: 22125

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

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

2) 15

Upvotes: 1

user684934
user684934

Reputation:

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.

Upvotes: 0

Tudor Constantin
Tudor Constantin

Reputation: 26861

That is just a short form for:

sum = sum + value;

Upvotes: 19

Related Questions