Open In App

How to Decrement a Variable by 1 in JavaScript ?

Last Updated : 29 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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.

Javascript




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.

Javascript




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

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads