How to Decrement a Variable by 1 in JavaScript ?
Last Updated :
29 Jan, 2024
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);
console.log(y);
|
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);
console.log(b);
|
Share your thoughts in the comments
Please Login to comment...