Programming Search⌘K

Featureintro

Increment and decrement

Also called ++, --, increment, decrement, prefix increment, postfix increment, update expression.

x++ and x-- add or subtract one and give the old value; ++x and --x give the new value. The operand must be a variable or a property. The value is converted to a number or a BigInt first.

Language
JavaScript

Synopsis

Form Effect on x Value
x++ x becomes x + 1 the old value
++x x becomes x + 1 the updated value
x-- x becomes x - 1 the old value
--x x becomes x - 1 the updated value

"If x is 3, then ++x sets x to 4 and returns 4, whereas x++ returns 3 and, only then, sets x to 4."1 ECMA-262 calls these forms update expressions.2

Semantics

r1. Prefix and postfix. "If used postfix, with operator after operand (for example, x++ ), the increment operator increments and returns the value before incrementing."3 "If used prefix, with operator before operand (for example, ++x ), the increment operator increments and returns the value after incrementing."3 -- works the same way with subtraction.4 Example: forms.

r2. Numbers and BigInts. "It first coerces the operand to a numeric value and tests the type of it. It performs BigInt increment if the operand becomes a BigInt; otherwise, it performs number increment."3 So a string variable that holds 5 becomes the number 6, and even the old value that x++ returns is a number. null becomes 1, and a word becomes NaN. Example: forms.

r3. References only. "The increment operator can only be applied on operands that are references (variables and object properties; i.e., valid assignment targets )."3 ECMA-262 makes an invalid target an early error, so 5++ fails before the code runs.2 "++x itself evaluates to a value, not a reference, so you cannot chain multiple increment operators together."3 Example: chained.

r4. No line break before postfix. The grammar has "[no LineTerminator here]" before a postfix ++ or --.2 So a line break before ++ ends the statement, and ++ applies to the next line (Automatic semicolon insertion). Example: uses.

r5. Side effects. An update expression writes to its operand, like an assignment (Assignment operators). In log[i++] = v, the index is the old i, and i grows by one. Example: uses.

Examples

The examples run with Node.js 24.

Prefix and postfix forms, a BigInt, and conversion of a string, null and a word (rules r1 and r2):

let x = 3;
const post = x++;
console.log(post, x);
const pre = ++x;
console.log(pre, x);
console.log(x--, --x, x);

let big = 3n;
big++;
console.log(big);

let text = "5";
const old = text++;
console.log(typeof old, old, typeof text, text);

let empty = null, word = "five";
empty++;
word++;
console.log(empty, word);
Output
3 4
5 5
5 3 3
4n
number 5 number 6
1 NaN

A counter on a property, an index that moves forward, and a line break before ++ (rules r4 and r5). In the last part, a stays 1 and b becomes 3:

const counts = { a: 0 };
const items = ["a", "b", "a"];
for (const item of items) {
  counts[item] ??= 0;
  counts[item]++;
}
console.log(counts);

let i = 0;
const log = [];
log[i++] = "first";
log[i++] = "second";
console.log(log, i);

let a = 1
let b = 2
a
++b
console.log(a, b);
Output
{ a: 2, b: 1 }
[ 'first', 'second' ] 2
1 3

Two increments in a chain (rule r3):

This code raises SyntaxError, on purpose.

let x = 1;
console.log(++(++x));

Node.js reports SyntaxError: Invalid left-hand side expression in prefix operation.

Pitfalls

The same variable twice in one expression

i++ + i++ is defined, because operands run from left to right (Expressions and operators), but it is hard to read. Use one update per statement.

x++ as a value

return count++; returns the old count. If the updated count is meant, write ++count or count += 1.

A const counter

const n = 0; n++; throws a TypeError, like any assignment to a const (Assignment operators). Use let for a counter.

Comparison

Topic JavaScript Python
increment x++, ++x3 x += 1, a statement
++x adds one two unary plus operators: +(+x)5

Python's unary operators are -, + and ~, and it has no ++.5 So ++x in Python is valid but does not change x: "The unary + (plus) operator yields its numeric argument unchanged".5 See Augmented assignment.

References

  1. Expressions and operators, MDN JavaScript Guide, Arithmetic operators
  2. ECMAScript 2026 Language Specification, 13.4 Update Expressions; 13.4.1 Static Semantics, Early Errors
  3. Increment (++), MDN, introduction; Description
  4. Decrement (--), MDN, introduction; Description
  5. Expressions, The Python Language Reference, 6.6 Unary arithmetic and bitwise operations

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW3 standard
  • a history with a source