Learn JavaScript
❯ operators.op_assignment

Assignment & Logical Assignment in JavaScript

$concept

Plain = stores, with destructuring forms ([a, b] = [b, a] swaps without temporaries) and chained assignment (a = b = 0, right-associative). Compound operators (+=, -=, *=, /=, %=, **=) fold operations into stores, and ES2021 logical assignment (||=, &&=, ??=) folds the guard idioms: count ??= 10 assigns only when nullish.

Assignment is an expression yielding the stored value β€” enabling chains and while ((line = next()) !== null) loops β€” but = inside conditions without extra parentheses is a lint error in every sane config.

$how it works
1

= stores and yields

a = b = 0 chains right to left; the expression evaluates to the stored value for embedding (sparingly).

2

Compound folds operations

x += 1, s *= 2, p **= 3 β€” operation plus store with the left side evaluated once.

3

Logical assignment guards

a ||= d (assign if falsy), a &&= f (assign if truthy), a ??= d (assign if nullish) β€” ??= is the precise initializer.

4

Destructuring assigns

[a, b] = [b, a] swaps; ({x} = obj) extracts β€” assignment shapes beyond the single name.

$example
let a = 1;
a += 2;
let count;
count ??= 10;
let x = 1, y = 2;
[x, y] = [y, x];
console.log(a, count, x, y);
Output
3 10 2 1
1a += 2 folds addition into the store β€” 3.
2count starts undefined, so ??= assigns 10 (a 0 would have survived).
3Destructuring swaps x and y with no temporary β€” 2 1 prints.
$use cases
Initializing caches and counters precisely with ??=
Swapping and rotating values via destructuring
Folding guarded defaults (||=) where broad falsy defaulting is intended
Reading chained assignments right-to-left during review

Recommended resourcesAffiliate links Β· may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice β€” pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from β€” great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in JavaScript yourself β€” building beats watching.