Other than the basic operators, JavaScript also has some language-specific operators. Two of these are increment
(denoted by ++
) and decrement
(denoted by --
) operators. The increment operator increases the value of the number by 1, and the decrement operator decreases the value of the number by 1. However, the order of this operator is important. If the increment or decrement operator is placed before the variable name, it is known as the pre-increment
or pre-decrement
operator and the increment or decrement in value occurs before returning it. If the increment or decrement operator is placed after the variable name, it is known as the post-increment
or post-decrement
operator and the increment or decrement in value occurs after returning it.
Let's see some examples.
xxxxxxxxxx
var w = 10;
var x = ++w;
console.log(x); // prints 11
var y = x--;
console.log(y); // prints 11
var z = --y;
console.log(z); //prints 10