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

Increment and Decrement Operators

Use ++ and -- to increase or decrease values by one, understanding prefix vs postfix behavior.

Increment and Decrement Operators

You'll learn how to use shorthand operators to increase or decrease a variable's value by exactly one.

The Basics

Instead of writing x = x + 1 or x = x - 1, JavaScript provides two convenient shortcuts:

  • ++ increases a value by 1 (increment)
  • -- decreases a value by 1 (decrement)

These operators are like elevator buttons: press "up" to go one floor higher, "down" to go one floor lower.

Prefix vs Postfix: When the Change Happens

Here's where it gets interesting—where you place the operator matters:

Postfix (after the variable): x++ or x--

  • Returns the old value first, then increments/decrements
  • Think: "use it, then change it"

Prefix (before the variable): ++x or --x

  • Increments/decrements first, then returns the new value
  • Think: "change it, then use it"
let a = 5;
let b = a++;  // b gets 5, then a becomes 6
// a is now 6, b is 5

let x = 5;
let y = ++x;  // x becomes 6, then y gets 6
// x is now 6, y is 6

When They're the Same

If you're using the operator by itself (not in an expression), prefix and postfix do the exact same thing:

let count = 10;
count++;  // count is now 11
// Same as:
++count;  // count is now 12

Key Takeaway: Use ++ and -- as shortcuts to change values by one; prefix changes before returning the value, postfix changes after—but when used alone, both work identically.