Open In App

How to Decrement a Variable by 1 in JavaScript ?

In JavaScript, you can decrement a variable by 1 using the decrement operator (--). Similar to incrementing, there are two ways to use the decrement operator:

Post-decrement

The current value of the variable is used in the expression, and then the variable is decremented.



Example: Using Post-decrement.




let x = 5;
let y = x--;
 
console.log(x); // Outputs 4 (original value decremented)
console.log(y); // Outputs 5 (original value used in the expression)

Output

4
5

Pre-decrement

The variable is decremented first, and then the updated value is used in the expression.

Example: Using Pre-decrement.




let a = 10;
let b = --a;
 
console.log(a); // Outputs 9 (original value decremented)
console.log(b); // Outputs 9 (updated value used in the expression)

Output
9
9
Article Tags :