Simple Assignment
The `=` operator assigns a value to a variable. It is the base for every other assignment operator.
Assignments evaluate from right to left, so `a = b = 5` sets both values to 5.
JavaScript Tutorial
Assignment operators set or update values stored in variables. They are used everywhere from counters to state updates.
Beyond `=`, JavaScript offers compound and logical assignment operators to keep updates concise and intentional.
Writing updates clearly prevents bugs and makes code easier to scan. Compound and logical assignments reduce repetition.
Knowing the difference between ||= and ??= avoids accidentally overwriting valid values like 0 or empty strings.
x = y
x += y, x -= y, x *= y, x /= y, x %= y, x **= y
x &&= y, x ||= y, x ??= y
x &= y, x |= y, x ^= y, x <<= y, x >>= y, x >>>= ylet total = 10;
total += 5; // 15
total -= 3; // 12
total *= 2; // 24
total /= 4; // 6
console.log(total);Compound assignment keeps updates short and readable.
let value = 3;
value **= 2; // 9
value %= 5; // 4
console.log(value);Use **= for powers and %= for cycling or wrapping values.
The `=` operator assigns a value to a variable. It is the base for every other assignment operator.
Assignments evaluate from right to left, so `a = b = 5` sets both values to 5.
Compound operators combine math with assignment, like `+=` or `*=`.
They keep code concise and reduce repetition when updating counters, totals, and scores.
Logical assignment operators (`&&=`, `||=`, `??=`) update a variable only when a logical condition is met.
They are great for default values and guard-style updates without extra if-statements.
Bitwise assignment operators (like `&=` or `<<=`) combine bitwise operations with assignment.
These are mainly used for flags, permissions, and low-level optimizations.
let name = "";
name ||= "Guest"; // empty string is falsy, so set fallback
let score = 0;
score ||= 10; // score becomes 10 (because 0 is falsy)
let count = 0;
count ??= 5; // count stays 0 because it's not null/undefined
console.log(name, score, count);Use ||= and &&= for truthy/falsy checks, and ??= for nullish-only checks.
const user = { id: 7, role: "admin" };
const { id, role } = user;
console.log(id, role);Destructuring is a readable way to assign multiple variables at once.
Without
let total = 10;
total = total + 5;
let name = "";
if (!name) {
name = "Guest";
}With
let total = 10;
total += 5;
let name = "";
name ||= "Guest";Prefer ??= to preserve 0, false, and empty strings.
Chaining like a = b = c can reduce readability.
Bitwise ops convert values to 32-bit integers.
Remember assignments evaluate from right to left.
||= assigns on any falsy value, while ??= assigns only for null or undefined.
It makes updates shorter and easier to read.
Yes, the assigned value, which enables chaining (though it can hurt clarity).
Practice: Build a score tracker that updates total points using compound assignment.
let score = 0;
// Add 10, subtract 3, then double
One Possible Solution
let score = 0;
score += 10;
score -= 3;
score *= 2;
console.log(score);It combines an operation with assignment, like `x += 2` instead of `x = x + 2`.
Use it when you only want to assign a default for null or undefined values.
Yes. The assignment expression evaluates to the assigned value.
Yes, destructuring assigns values to variables from arrays or objects.
Run the assignment examples and edit the values to see how each operator updates state.