01 / 20
+= -= *

Assignment
Operators

Master the shorthand operators that make updating variables in JavaScript faster, cleaner, and less error-prone.

JavaScript

What Is Assignment?

In JavaScript, assignment means storing a value in a variable. The most basic assignment operator is the equals sign =. It takes the value on the right and places it into the variable on the left.

let score = 0; // score now holds 0
score = 10; // score now holds 10

The Problem: Repetition

When you want to update a variable based on its current value, you end up writing the variable name twice:

score = score + 5; // add 5 to score
lives = lives - 1; // subtract 1 from lives
price = price * 0.9; // apply 10% discount

The Solution: Compound Assignment

Compound assignment operators combine an arithmetic operation with assignment into a single step. They read the current value, perform the operation, and store the result: all in one expression.

score += 5; // same as score = score + 5
lives -= 1; // same as lives = lives - 1
price *= 0.9; // same as price = price * 0.9

The Core Rule

x = x op value Long form becomes x op= value Shorthand score = score + 5 score += 5 Pattern: operator + equals sign

The Five Operators You Need

Operator Shorthand Equivalent Purpose
+= x += 3 x = x + 3 Addition
-= x -= 3 x = x - 3 Subtraction
*= x *= 3 x = x * 3 Multiplication
/= x /= 3 x = x / 3 Division
%= x %= 3 x = x % 3 Remainder

= vs +=: Key Distinction

= Replaces
Overwrites the old value entirely. The previous value is lost.
+= Updates
Reads the old value, adds to it, then stores the new total.
let x = 10;

x = 5; // x is now 5 (10 is gone)

let y = 10;
y += 5; // y is now 15 (10 + 5)

How to Apply Them

1
Identify the pattern. Look for x = x op value anywhere the same variable appears on both sides.
2
Pick the right operator. Match the arithmetic: + becomes +=, - becomes -=, and so on.
3
Write the shorthand. Remove the duplicate variable name and combine the operator with =.
4
Verify mentally. Expand it back to long form in your head to confirm it does what you expect.

Example 1: Score Counter

A game starts at 0 points. The player earns 25 points, then 10 more, then loses 5.

let score = 0;

score += 25; // 0 + 25 = 25
score += 10; // 25 + 10 = 35
score -= 5; // 35 - 5 = 30

console.log(score); // 30

Example 2: Discount Calculator

A product costs $200. Apply a 15% discount, then add $12 shipping.

let price = 200;

price *= 0.85; // 200 * 0.85 = 170
price += 12; // 170 + 12 = 182

console.log(price); // 182

Example 3: Remainder in Action

The remainder operator %= is useful for wrapping values. Imagine a clock-style counter that cycles from 0 to 3:

let counter = 0;

counter += 1; // 1
counter += 1; // 2
counter += 1; // 3
counter += 1; // 4
counter %= 4; // 4 % 4 = 0 (wraps back!)

console.log(counter); // 0

+= with Strings

The += operator also works with strings: it concatenates (joins) text onto the existing value.

let greeting = "Hello";

greeting += " "; // "Hello "
greeting += "World"; // "Hello World"

console.log(greeting); // "Hello World"

How Values Flow

STEP 1 let x = 10 STEP 2 x += 5 STEP 3 x *= 2 x = 10 initial value x = 15 10 + 5 x = 30 15 * 2 Each step reads, computes, and stores the new value

Common Mistakes

✕ Reversed Order
Writing =+ instead of +=. The expression x =+ 5 means "assign positive 5" (like x = +5), not addition.
✕ Using on const
Variables declared with const cannot be reassigned. Using += on a const throws a TypeError.
✕ Mixing Types
Using += with a number and a string concatenates instead of adding: 5 += "2" gives "52", not 7.

Summary Table

Operator Example Long Form Result (x=10)
+= x += 3 x = x + 3 13
-= x -= 3 x = x - 3 7
*= x *= 3 x = x * 3 30
/= x /= 4 x = x / 4 2.5
%= x %= 3 x = x % 3 1

Question 1 of 5

What is the value of x after this code runs?

let x = 8; x += 4;

Question 2 of 5

Which of the following is equivalent to total = total * 3?

Question 3 of 5

What does count %= 5 do when count is 13?

Question 4 of 5

What is the final value of n?

let n = 20; n -= 8; n /= 3;

Question 5 of 5

What happens when you run let msg = "Hi"; msg += " there";?