JavaScript

This page explains how JavaScript assignment operators store a value in a variable and update the value with a calculation.

Goal: use = to assign a value, then choose a compound assignment operator when updating the current value.

Assignment operators

An assignment puts the value on the right into the variable on the left. The simplest assignment operator is =.

JavaScript

let score = 5;
score = 8; // score is now 8

The right-hand expression is evaluated first, then its result is stored in the variable.

Compound assignment operators

Compound operators combine a calculation with an assignment. For example, score += 3 means approximately score = score + 3.

OperatorMeaningExample
+=add and assignx += 3
-=subtract and assignx -= 3
*=multiply and assignx *= 3
/=divide and assignx /= 3
%=remainder and assignx %= 3

JavaScript

let count = 10;
count += 2; // 12
count -= 3; // 9
count *= 2; // 18
count /= 3; // 6
count %= 4; // 2

Things to remember

Related topics