JS Assignment
In this page:
variable = value;
variable += value; // also -=, *=, /=, %=, **=
Basic Compound Assignment
+=, -=, *=, and /= each combine an arithmetic operation with assignment, updating a variable based on its own current value in a single, compact statement.
Example: Basic Compound Assignment
// Declare the variable `total`, set to `10`
// Declare the variable `total`, set to `10`
let total = 10;
total += 5;
total -= 2;
total *= 2;
total /= 4;
// Print `total` to the console
// Print `total` to the console
console.log(total);
Remainder and Exponent Assignment
%= applies the remainder operation and assigns the result back, and **= applies exponentiation and assigns the result back, following the exact same compact pattern as the other compound operators.
Example: Remainder and Exponent Assignment
// Declare the variable `counter`, set to `7`
// Declare the variable `counter`, set to `7`
let counter = 7;
counter %= 3;
// Print `counter` to the console
// Print `counter` to the console
console.log(counter);
// Declare the variable `base`, set to `2`
// Declare the variable `base`, set to `2`
let base = 2;
base **= 3;
// Print `base` to the console
// Print `base` to the console
console.log(base);
Logical AND Assignment (&&=)
&&= only assigns a new value to a variable if that variable is currently truthy, leaving it unchanged if it's already falsy, useful for updating a value only when it's meaningfully present.
Example: Logical AND Assignment (&&=)
let session = "active";
session &&= "updated";
console.log(session); // "updated"
let empty = "";
empty &&= "updated";
console.log(empty); // "" stays unchanged
Logical OR Assignment (||=)
||= assigns a new value only if the variable is currently falsy, commonly used to provide a default value for something that might be missing, empty, or zero.
Because 0 and '' are falsy too, ||= can overwrite values you actually wanted to keep, which is exactly why ??= exists.
Example: Logical OR Assignment (||=)
let username = "";
username ||= "Guest";
console.log(username); // "Guest", "" is falsy
Nullish Coalescing Assignment (??=)
??= assigns a new value only if the variable is currently null or undefined, unlike ||=, it treats other falsy values like 0 and empty strings as valid and leaves them untouched.
Example: Nullish Coalescing Assignment (??=)
let count = 0;
count ??= 10;
console.log(count); // 0 stays, only null/undefined trigger ??=
let missing;
missing ??= 10;
console.log(missing); // 10
- Writing count =+ 1 instead of count += 1, the space and operator order matters, =+ is actually assignment followed by unary plus.
- Using ??= when you actually wanted ||=, ?? only treats null and undefined as missing, while || treats any falsy value as missing.
- Forgetting that compound assignment operators modify the variable in place, they don't create a new variable.
- Compound operators like +=, -=, *=, /=, and %= combine an operation with assignment in one step.
- **= applies exponentiation and assignment together, following the same pattern as the other compound operators.
- &&=, ||=, and ??= conditionally assign a value only when certain logical conditions about the current value are met.
Standard compound assignment operators work in every browser; &&=, ||=, and ??= are modern additions supported in all current browser versions.
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: