assignment operators (=, +=)
In JavaScript, the assignment operators (=, +=) store a value in a variable or property and evaluate to the value they stored. The plain assignment operator (=) writes its right operand, while each compound form, such as the addition assignment operator (+=), first combines the current value with the right operand.
Assignment is an expression, so it has a value. Chained assignments group from the right, which makes a = b = 0 mean a = (b = 0). A compound form like x += y behaves like x = x + y, except that the target x is evaluated only once. The left side of = can also be a destructuring pattern.
JavaScript Assignment Operators Syntax
Every assignment operator takes a target on the left and an expression on the right:
<target> = <expression>
<target> <operator>= <expression>
Starting from let x = 10;, each compound operator gives this result:
| Operator | Name | Example | Result |
|---|---|---|---|
+= |
Addition assignment | x += 5 |
15 |
-= |
Subtraction assignment | x -= 3 |
7 |
*= |
Multiplication assignment | x *= 2 |
20 |
/= |
Division assignment | x /= 4 |
2.5 |
%= |
Remainder assignment | x %= 3 |
1 |
**= |
Exponentiation assignment | x **= 2 |
100 |
<<= |
Left shift assignment | x <<= 1 |
20 |
>>= |
Right shift assignment | x >>= 1 |
5 |
>>>= |
Unsigned right shift assignment | x >>>= 1 |
5 |
&= |
Bitwise AND assignment | x &= 6 |
2 |
|= |
Bitwise OR assignment | x |= 5 |
15 |
^= |
Bitwise XOR assignment | x ^= 3 |
9 |
Each compound form applies the matching arithmetic, bitwise, or shift operator. The logical assignment operators (&&=, ||=, ??=) follow different rules, because they can skip the write.
JavaScript Assignment Operators Examples
Say a scoreboard tracks two teams. Compound operators add points in place, and a chained assignment resets both scores at once:
> let homeScore = 0;
undefined
> let awayScore = 0;
undefined
> homeScore += 3
3
> awayScore += 2
2
> homeScore = awayScore = 0
0
> [homeScore, awayScore]
[ 0, 0 ]
The inner awayScore = 0 runs first, and its result, 0, becomes the right operand of the outer assignment.
Say a page counter looks up its key with a function that logs each call. The compound form evaluates the target once, while the spelled-out form evaluates it twice:
> const visits = { home: 0 };
undefined
> function currentPage() {
… console.log("Looking up page");
… return "home";
… }
undefined
> visits[currentPage()] += 1
Looking up page
1
> visits[currentPage()] = visits[currentPage()] + 1
Looking up page
Looking up page
2
The difference shows whenever the target expression has side effects.
JavaScript Assignment Operators Gotchas
These traps show up in real code:
- A single
=inside a condition, such asif (status = "done"), assigns the value and then tests it, so the branch runs whenever the assigned value is truthy. - The
+=operator concatenates when either operand is astring, solet quantity = "3"; quantity += 1;leaves"31". - Any assignment to a
constbinding, compound forms included, throws aTypeError. - In strict mode, assigning to an undeclared name throws a
ReferenceErrorinstead of creating a global property.
JavaScript Assignment Operators Use Cases
Common uses of the assignment operators include:
- Initializing and updating variables and object properties
- Accumulating a running total in a loop with
+= - Appending text to a string with
+= - Scaling a value in place with
*=or/= - Setting flag bits with
|=and clearing them with&=
Conditional writes, such as filling in a missing default, use the logical assignment operators instead.
Related Resources
Course
JavaScript Variables and Types for Pythonistas
Port a print shop's Python backup check to JavaScript, and learn how const, let, typeof, null, undefined, empty values, and === differ from the Python types you know.
Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.
By Leodanis Pozo Ramos • Updated Nov. 12, 2026