JAVASCRIPT
JavaScript -- Operator: Syntax, Usage, and Examples
In JavaScript, the -- operator is used to decrease the value of a variable by one. It’s called the decrement operator, and it comes in two forms: postfix and prefix.
How to Use the -- Operator in JavaScript
The syntax is straightforward and resembles basic arithmetic operations. You can use the -- operator either after or before a variable:
Learn JavaScript on Mimo
JSX
let x = 10;
x--; // Postfix: subtracts 1 after the current expression is evaluated
let y = 10;
--y; // Prefix: subtracts 1 before the current expression is evaluated
- Postfix (
x--): The variable is used in the expression first, then decreased. - Prefix (
-x): The variable is decreased first, then used in the expression.
When to Use the -- Operator in JavaScript
The -- operator is useful in many common coding scenarios, especially when you're working with counters or loops. Here are some typical use cases:
1. Countdown Loops
When you want to loop backward, the -- operator makes your code concise and readable.
JSX
for (let i = 5; i > 0; i--) {
console.log(i);
}
2. Updating Scores or Stats
If you’re building a game or app that tracks quantities like lives or resources, use -- to subtract one quickly.
JSX
let lives = 3;
lives--; // Player loses a life
3. Manual Decrement
Sometimes you just want to manually subtract 1 from a variable, such as adjusting UI element states or debugging values.
JSX
let loadingSteps = 4;
loadingSteps--;
Examples of -- in JavaScript
Example 1: Postfix Decrement
JSX
let apples = 5;
let basket = apples--;
console.log(basket); // 5
console.log(apples); // 4
The variable basket stores the original value of apples, then apples is decremented afterward.
Example 2: Prefix Decrement
JSX
let oranges = 5;
let basket = --oranges;
console.log(basket); // 4
console.log(oranges); // 4
Here, oranges is decremented first before being assigned to basket.
Example 3: Decrementing in a While Loop
JSX
let countdown = 3;
while (countdown > 0) {
console.log(`Launching in ${countdown}...`);
countdown--;
}
This example mimics a countdown timer.
Learn More About the -- Operator in JavaScript
Prefix vs Postfix Behavior
Understanding the difference between --x and x-- can prevent subtle bugs, especially in expressions.
JSX
let score = 10;
console.log(--score); // Outputs: 9 (score is decremented before logging)
console.log(score--); // Outputs: 9 (score is logged, then decremented)
console.log(score); // Outputs: 8
This difference becomes even more important when you use the -- operator inside more complex expressions, such as function arguments or array indexing.
Using -- with Functions
JSX
function showValue(value) {
console.log(value);
}
let stars = 3;
showValue(stars--); // Logs: 3
showValue(--stars); // Logs: 1
Again, postfix sends the current value, while prefix changes it before passing it to the function.
Common Mistakes to Avoid
- Don't mix
-with assignment confusingly. It’s easy to forget when the decrement happens. - Don’t apply
-to constants. Trying5--will throw an error, as constants and literals can't be reassigned.
Decrementing Non-Numbers
While JavaScript allows weird things, the -- operator expects numeric values. If applied to strings or booleans, JavaScript will try to convert them to numbers:
JSX
let str = "5";
str--; // Works, becomes 4
let bool = true;
bool--; // true becomes 1, then 0
let invalid = "hello";
invalid--; // NaN
In these cases, avoid implicit type coercion unless you know what you're doing.
The -- operator is a handy shortcut for subtracting one from a value. Use it when looping backward, managing game states, or adjusting counters. Just remember the difference between prefix and postfix forms to avoid confusion in more complex scenarios.
This operator is small but mighty, and once you understand how it works, you'll spot it everywhere in JavaScript.
Join 35M+ people learning for free on Mimo
4.8 out of 5 across 1M+ reviews
Check us out on Apple AppStore, Google Play Store, and Trustpilot