The assignment operators are used to assign values to variables. They start with the very simple
= and move on to
–=, and so on.
- The operator
+=adds the value on the right side to the variable on the left, instead of totally replacing the value on the left.
Thus, if count starts with the value 6, the statement.
count += 1
sets count to 7, just like the more familiar assignment statement:
count = count + 1
+= – to sum up the left and right operand values then assign the obtained result to left operand.
-=– Subtraction assignment.
/=– Division assignment.
*=– Multiplication assignment.
%=– Modulus (Division Remainder) assignment.
let x = 5; x += 5; // x now equals 9, same as writing x = x + 5; x -= 4; // x now equals 6, same as writing x = x - 4; x *= 2; // x now equals 12, same as writing x = x * 2; x /= 5; // x now equals 4, same as writing x = x / 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;