Skip to content

increment and decrement (++, --)

In JavaScript, the increment and decrement operators (++, --) add one to or subtract one from a variable or property and store the result back. The prefix form (++x) returns the new value, while the postfix form (x++) returns the value from before the change.

Both operators convert the operand to a numeric value first, so they work on number and bigint values and turn a numeric string into a number. The operand has to be an assignment target, such as a let variable or an object property.

JavaScript Increment and Decrement Syntax

Each operator has a prefix and a postfix form:

Language: JavaScript Syntax
++<target>
<target>++
--<target>
<target>--

The four forms give these results when x starts at 5:

Operator Name Example Result
++ Prefix increment ++x 6
++ Postfix increment x++ 5
-- Prefix decrement --x 4
-- Postfix decrement x-- 5

In every row, x ends up at 6 after an increment and 4 after a decrement. Only the value of the expression differs.

JavaScript Increment and Decrement Examples

Say a page tracks how many times an article was viewed. Each form adds one to the counter, but the expression’s value depends on where ++ sits:

Language: JavaScript
> let views = 41;
undefined

> views++
41
> views
42
> ++views
43

The postfix form reports 41 and then updates the counter. The prefix form updates it first and reports 43.

Say a retry loop counts down the attempts it has left. Testing retries-- checks the old value, so the loop body sees the value after the decrement:

Language: JavaScript
> let retries = 3;
undefined

> while (retries--) console.log(`Retrying, ${retries} left`);
Retrying, 2 left
Retrying, 1 left
Retrying, 0 left
undefined
> retries
-1

The loop runs three times, and the final failed check still decrements, which leaves retries at -1.

JavaScript Increment and Decrement Gotchas

  • A quantity read from a form as "3" becomes the number 4 after quantity++, while quantity += 1 gives the string "31", because the addition operator joins strings.
  • The operand must be assignable, so 5++ and ++(++x) both throw a SyntaxError, and incrementing a const binding throws a TypeError.
  • A line break can’t come between an operand and a postfix ++ or --, so automatic semicolon insertion reads the operator as a prefix on the next line.

JavaScript Increment and Decrement Use Cases

  • Advancing the counter in a for loop with i++
  • Counting events such as clicks, views, or failed attempts
  • Counting down remaining retries or items in a while loop
  • Reading the current index and advancing it in one step, as in items[index++]
A developer pulls a Math.floor() lever on a counting machine with a coffee sack on its scale, a big dial, a % gauge and a toFixed(2) receipt printer.

Course

JavaScript Numbers for Pythonistas

Port a coffee roastery's batch planner from Python to JavaScript, and learn to divide, round, read numbers from text, and format them with JavaScript's one number type.

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