Increment and decrement (++ --)
Increases or decreases a variable by one.
Available since: ES1
Syntax
x++ / ++x / x-- / --xExamples
Increasing and decreasing:
let count = 5;
count++;
count++;
count--;
console.log(count);Output:
6The postfix and prefix forms return different values:
let a = 1;
let b = 1;
console.log(a++);
console.log(++b);
console.log(a, b);Output:
1
2
2 2Notes
To avoid confusion, use ++ on its own line, not inside other expressions. For steps other than one use x += 2. It does not work with const: declare the variable with let.
See also
- Remainder (%) — The remainder of a division.
- let — Declares a variable whose value can change.
In the tutorial
- Operators — JavaScript arithmetic operators, the remainder, operator precedence, compound assignment, ++ and --.