Course contentsShow
← JavaScript
Lesson 248 of 2,7237. Arithmetic and OperatorsFree lesson

Remainder and Exponentiation Assignment

Use %= for modulo assignment and **= for exponentiation assignment in numeric calculations.

What You'll Learn

You'll learn how to update numeric variables using remainder assignment (%=) and exponentiation assignment (**=) — two shortcuts that combine math operations with assignment.

The Core Idea

Just like you've seen += combine addition and assignment, JavaScript provides similar shortcuts for two other arithmetic operations:

  • %= (remainder assignment) — divides a variable by a value and stores the remainder
  • **= (exponentiation assignment) — raises a variable to a power and stores the result

These operators modify the variable in place, just like the other assignment operators you've learned.

Remainder Assignment (%=)

Instead of writing x = x % 3, you can write:

let tickets = 17;
tickets %= 5;  // Same as: tickets = tickets % 5
// tickets is now 2 (the remainder of 17 ÷ 5)

Common use case: Wrapping numbers around a limit (like cycling through array indices or creating repeating patterns).

Exponentiation Assignment (**=)

Instead of writing x = x ** 2, you can write:

let base = 3;
base **= 4;  // Same as: base = base ** 4
// base is now 81 (3 to the 4th power)

Common use case: Repeatedly squaring a number or applying exponential growth calculations.

Why Use These?

These shortcuts make your code more concise and signal your intent clearly — you're updating a value, not creating a new one. They're especially useful in loops or calculations where you're progressively modifying the same variable.

Key Takeaway: Use %= to update a variable with its remainder after division, and **= to update a variable with its value raised to a power — both modify the variable in place.