Skip to content

Try our new Crash Courses!

Buy one of our new Crash Courses, now hosted on Podia.

Assignment operators – JavaScript

Length: 10 minutes

Summary

+=
-=
*=
/=
%=
**=

Details

The assignment operators modify the value of a variable.

Addition assignment

The addition assignment operator takes the number on the right, adds it to the value stored inside the variable, and stores the new value inside the variable.

let x = 1;
x += 2; // 3

Subtraction assignment

The subtraction assignment operator takes the number on the right, subtracts it from the value stored inside the variable, and stores the new value inside the variable.

let x = 1;
x -= 2; // -1

Multiplication assignment

The multiplication assignment operator takes the number on the right, multiplies it with the value stored inside the variable, and stores the new value inside the variable.

let x = 1;
x *= 2; // 2

Division assignment

The division assignment operator takes the number on the right, divides the value stored inside the variable by it, and stores the new value inside the variable.

let x = 1;
x /= 2; // 0.5

Remainder assignment

The remainder assignment operator takes the number on the right, divides the value stored inside the variable by it, and stores the remainder inside the variable.

let x = 1;
x %= 2; // 0

Exponentiation assignment

The exponentiation assignment operator raises the value stored inside the variable to the power of the number on the right, and stores the new value inside the variable.

let x = 3;
x **= 2; // 9

Demo

Exercises

Try the following statements in the console:

let x = 1
x += 2
x -= 2
x *= 2
x /= 2
x %= 2
x **= 2

The answers will be 3, 1, 2, 1, 1, and 1 (assuming you do each operation in order).

References

Expressions and operators on MDN

Back to: JavaScript Reference > JS Data Types