Skip to content

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:

Language: JavaScript Syntax
<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:

Language: JavaScript
> 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:

Language: JavaScript
> 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 as if (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 a string, so let quantity = "3"; quantity += 1; leaves "31".
  • Any assignment to a const binding, compound forms included, throws a TypeError.
  • In strict mode, assigning to an undeclared name throws a ReferenceError instead 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.

A puzzled developer with a clipboard studies an empty UNDEFINED bin beside STRING, NUMBER and NULL bins filled by a TYPEOF scanning machine.

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.

basics js py

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