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
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: