=
Assigns a value to a variable or property.
Assignment operators put values into names or properties. Compound assignment changes a value using its current value.
let items = 5; items += 1; // 6 items *= 12; // 72
Operators & Expressions
The basic assignment operator is =. It stores the right-hand value into the left-hand target. That target can be a variable, object property or array position.
Compound assignment combines an operation with assignment: +=, -=, *=, /=, %= and **=. Modern JavaScript also has logical assignment operators such as ||=, &&= and ??=.
Assignment expressions return a value, but professional code usually avoids hiding assignment deep inside other expressions. When state changes, make the change obvious.
Assigns a value to a variable or property.
Adds or subtracts and stores the result back into the same target.
Numeric compound assignments for calculations.
Logical assignment for defaults and conditional state updates.
Examples
let itemCount = 5;
const hasExtraItem = true;
if (hasExtraItem) {
itemCount += 1;
}
user.owner ??= "Default owner";
let itemCount = 5;
let total;
if ((total = itemCount * 12) > 1000) {
console.log("Large total");
}
// This works, but it hides a state change inside a condition.
Rules that matter
JavaScript operators are compact, but compact is not always clear. Convert values before comparing or calculating, use strict checks by default and add parentheses when a future reader would otherwise need to remember a precedence table.
A changed value should be easy to spot while reading.
count += 1 is clearer than count = count + 1.
Assignment inside conditions or function calls is legal, but often too clever.
It only assigns when the current value is null or undefined.
It overwrites valid falsy values such as 0 and empty strings.
Assignment is not a habit. It should communicate change.
Production thinking
Assignment changes program state. If state changes are hidden, debugging becomes guesswork.
Assignment often updates aria state, messages and form status. Hidden state changes can leave the visible UI out of sync.
Production code benefits from predictable, local state changes. Avoid assignment patterns that require mental gymnastics.
If assignment controls rendered content, unclear state updates can lead to missing or stale page data.
Live code lab
The preview runs inside an isolated iframe. The JavaScript is placed inside the HTML editor for now, so every example stays together and remains easy to understand.
Mini assignment
Practice assignment
Try it yourself
Self-check
Say the answer in plain language. If you can explain the operator behavior, you can debug the expression.