Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Expressions & Operators  ›  Lesson

Assignment Operators

Assignment Operators 12 minCompound assignment operators like += update a variable using its own current value; ||= fills a variable only when it's falsy, while ??= fills it only when it's null or undefined.

Assignment Operators

In short: = always overwrites. Compound operators like +=, -=, *= update a variable using its own current value — score += 5 means score = score + 5. Two newer operators only assign conditionally: ||= fills a variable in whenever it's falsy (0, "", false, null, undefined), while ??= fills it in only when it's null or undefined. That difference matters the moment a falsy-but- valid value, like 0, is a value you want to keep.

A conveyor belt dropping values into four boxes: vol = 70 (always overwrites), score += 5 (score = score + 5), name ||= "Guest" (box was falsy, fills it), vol ??= 50 (already 70, box refuses, stays 70) — with vol=0; vol ||= 50 becoming 50, but vol2=0; vol2 ??= 50 staying 0

Add points to a running score

+= is shorthand for "take the current value, do the operation, assign it back" — it reads and writes the same variable in one step:

javascript

score += 5 is exactly score = score + 5, so score becomes 15. Every arithmetic operator has this shorthand: -=, *=, /=, %=, **=.

Fill in a missing nickname

||= only assigns when the current value is falsy — it leaves a truthy value alone:

javascript

name starts as "", an empty string, which is falsy — so ||= replaces it with "Guest". If name had already held "Asha", a truthy string, ||= would have done nothing at all.

The trap: a muted volume treated as unset

0 is a perfectly valid, intentional value — but it's also falsy, which is exactly where ||= goes wrong and ??= is the fix:

javascript

vol ||= 50 treats the deliberately-muted 0 as "nothing set" and overwrites it with 50 — the user's mute is lost. vol2 ??= 50 only checks for null/undefined, and 0 is neither, so vol2 correctly stays 0. Whenever 0, "", or false are meaningful values you want to preserve, reach for ??=, not ||=.

Quick practice

quick drill

What does let score = 10; score += 5; console.log(score); print?

quick drill

What does let name = ""; name ||= "Guest"; console.log(name); print?

quick drill

What does let vol = 0; vol ||= 50; console.log(vol); print?

quick drill

What does let vol = 0; vol ??= 50; console.log(vol); print?

quick drill

Which operator only assigns when the left side is null or undefined?

🎯 Your turn

A volume slider starts at 0 (muted). Code runs volume ||= 50; to set a default when nothing has been chosen yet. Explain what goes wrong, and rewrite it so a genuine 0 is preserved.

Write your explanation and submit it — there's no code to run here, just your reasoning.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainZero is not empty

A volume slider starts at 0 (muted). Code runs volume ||= 50; to set a default when nothing has been chosen yet. Explain what goes wrong, and rewrite it so a genuine 0 is preserved.