# Assignment operators

Expressions use assignment operator (= sign) to assign values. JavaScript supports other assignment operators that we can use to assign values to a variable. Those other assignment operators can assign a value in single operation instead of two. If you have worked in a programming language like C++, they should be familiar to you. The following, for instance,

var x = 5;
var y = 15;
x += y;

sets x to 20 on line 3. The += operator simply adds the value of a variable that is on the left of the assignment operator to value of the variable that is on the right hand side of the assignment operator. Thus line 3 above is equivalent to

x = x + y;

Table 3 summarizes assignment operators available in JavaScript. If you are new to programming or programming language like JavaScript, you may want to consider avoiding them until you feel comfortable using them. Instead use the regular assignment operator (=) with a binary or unary arithmetic operators.

Table 3 Summary of assignment operators
Operator Description Example Equivalent to
= Assigns the value of a variable on the right hand side (of the =) to the variable on the left of = operator. a = b; a = b;
+= Adds two variables and assigns the result to a variable on the left (of +=). a += b; a = a + b;
-= Subtracts two variables and assigns the result to a variable on the left (of -=). a -= b; a = a - b;
/= Divides the variable on the left (of /=) by the variable on the right (of /=) and assigns the result to the variable on the left. a /= b; a = a / b;
*= Multiples the variable on the left (of *=) by the variable on the right (of *=) and assigns the result to the variable on the left. a *= b; a = a * b;
%= Divides the variable on the left (of %=) by the variable on the right (of %=) and assigns the remainder to the variable on the left. a %= b; a = a % b;