JavaScript Increment ++ and Decrement —

The increment and decrement operators in JavaScript will add one (+1) or subtract one (-1), respectively, to their operand, and then return a value.

Increment & Decrement operators

SYNTAX: Consider ‘x’ to be a variable:

Then,
Increment:  — x++ or ++x
Decrement:  — x– or –x

In the syntax we can see that the — and ++ have been used before and after the variable, so in terms of code, it might look similar to :

// Increment
let a = 1;
a++;
++a;
// Decrement
let b = 1;
b--;
--b;

Using ++/– Before the Variable

If you want to make the variable increment and decrement before using it, this is the way it has to be done, and in terms of code and example it is demonstrated below.

// Increment
let a = 1;
console.log(++a);    // 2
console.log(a);      // 2
// Decrement
let b = 1;
console.log(--b);    // 0
console.log(b);      // 0

As you can see, that the variable have been incremented/decremented before logging out the result in the first line.

Using ++/– After the Variable

If you want to make the variable increment and decrement after using it, this is the way it has to be done, and in terms of code and example it is demonstrated below.

// Increment
let a = 1;
console.log(a++);    // 1
console.log(a);      // 2
// Decrement
let b = 1;
console.log(b--);    // 1
console.log(b);      // 0

As you can see, that the variable have been incremented/decremented after logging out the result in the first line.

Conclusion:

Hopefully, my article was helpful, and by now you are clear how the increment and decrement operators works in any programming language.