How does += (plus equal) work?
JavascriptJavascript Problem Overview
I'm a bit confused with the += sign. How does it work?
-
1 += 2
// equals ? -
and this
var data = [1,2,3,4,5]; var sum = 0; data.forEach(function(value) { sum += value; }); sum = ?
Javascript Solutions
Solution 1 - Javascript
1 += 2
is a syntax error (left-side must be a variable).
x += y
is shorthand for x = x + y
.
Solution 2 - Javascript
- 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;
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
Solution 3 - Javascript
That is just a short form for:
sum = sum + value;
Solution 4 - Javascript
+=
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
.
Solution 5 - Javascript
+=
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
Solution 6 - Javascript
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 isReferenceError: invalid assignment left-hand side
. -
x += y
is the short form forx = x + y
, wherex
is the variable andx + 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][1] shortcuts works the same way (relatively to the standard operations they refer to). . [1]: https://developer.mozilla.org/en/JavaScript/Reference/Operators/Assignment_Operators
Solution 7 - Javascript
...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"
Solution 8 - Javascript
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/
Solution 9 - Javascript
a += b
is shorthand for a = a +b
which means:
-
1 += 2
// won't compile -
15
Solution 10 - Javascript
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;
Solution 11 - Javascript
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
.
Solution 12 - Javascript
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.
Solution 13 - Javascript
x += 1
is just shorthand for x = x + 1
It can also be used for strings:
var string = "foo"
string += "bar"
Solution 14 - Javascript
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
would logvar myVariable = 1 myVariable += 2 console.log(myVariable)
3
to the console, asx += y
is just short forx = x + y
-
would makevar data = [1,2,3,4,5] var sum data.forEach(function(value){ sum += value })
sum
= 15 because:sum += 1 //sum = 1 sum += 2 //sum = 3 sum += 3 //sum = 6 sum += 4 //sum = 10 sum += 5 //sum = 15