By the end of this lesson, you will be able to assign values in JavaScript using = and update them with compound operators such as +=, -=, and ??=.
What it is
Assignment operators store a value in a variable, property, or element. The simplest one is =, which replaces the current value. Compound assignment operators combine a calculation with assignment: x += 5 means “take the current value of x, add 5, and store the result back in x.” Related terms include left-hand side, the target being assigned to, right-hand side, the value or expression being used, and logical assignment operators such as &&=, ||=, and ??=.
Why it matters
- It reduces repetition when updating counters, totals, or state.
- It makes common patterns like accumulating values easier to read.
- Logical assignment provides concise default values.
- It helps avoid mistakes from writing the same variable name twice.
Syntax or steps
Basic assignment:
let x = 10;
Compound assignment:
x += 5;
For simple variables, JavaScript reads the current left-hand side value, evaluates the right-hand side, applies the operation, and assigns the result. Examples include -=, *=, /=, %=, and **=.
Logical assignment operators are conditional:
x ||= yassignsyonly ifxis falsy.x &&= yassignsyonly ifxis truthy.x ??= yassignsyonly ifxisnullorundefined.
Example
let score = 10;
score += 5;
score -= 3;
score *= 2;
score /= 4;
score %= 4;
score **= 3;
let user = null;
user ??= "guest";
let theme = "";
theme ||= "light";
console.log(score, user, theme);
Part by part:
score += 5changes10to15.score -= 3changes15to12.score *= 2changes12to24.score /= 4changes24to6.score %= 4changes6to2.score **= 3changes2to8.user ??= "guest"assigns"guest"becauseuserisnull.theme ||= "light"assigns"light"because""is falsy.
Common mistakes
- Using
||=when you mean??=.||=treats0,"", andfalseas missing values. Ifcount = 0,count ||= 5becomes5. Use??=to preserve valid falsy values. - Forgetting string concatenation. If
xis a string,x += 1produces concatenation, not numeric addition. Convert withNumber(x)when you need arithmetic. - Assigning to
const.const x = 1; x += 1;throws an error. Useletorvarfor values that change. - Confusing assignment with comparison.
if (x = 5)assigns5and is truthy. Use===for comparison.
When to use it
| Style | Example | Use when |
|---|---|---|
| Basic assignment | x = x + 5 | The expression is complex or clarity matters more than brevity. |
| Compound assignment | x += 5 | You are updating a variable with a simple operation. |
| Logical assignment | x ??= 5 | You want a default value only for null or undefined. |
Practice
Guided exercise: Predict the output.
let total = 20;
total += 10;
total /= 2;
console.log(total);
Expected output: 15.
Challenge: What does this log?
let count = 0;
count ||= 5;
console.log(count);
Hint: 0 is falsy, so ||= assigns 5. If you wanted to keep 0, use ??=.
Quick check
Question: What does x ??= 10 do?
Answer: It assigns 10 to x only if x is null or undefined.
Summary
Assignment operators let you store and update values efficiently. Compound forms such as += and *= reduce repetition, while logical forms such as ??= make default handling concise. Choose the form that keeps the intent clear and preserves valid values.