Assignment & Logical Assignment in JavaScript
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.
= stores and yields
a = b = 0 chains right to left; the expression evaluates to the stored value for embedding (sparingly).
Compound folds operations
x += 1, s *= 2, p **= 3 β operation plus store with the left side evaluated once.
Logical assignment guards
a ||= d (assign if falsy), a &&= f (assign if truthy), a ??= d (assign if nullish) β ??= is the precise initializer.
Destructuring assigns
[a, b] = [b, a] swaps; ({x} = obj) extracts β assignment shapes beyond the single name.
let a = 1;a += 2;let count;count ??= 10;let x = 1, y = 2;[x, y] = [y, x];console.log(a, count, x, y);Recommended resourcesAffiliate links Β· may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in JavaScript yourself β building beats watching.