What is decrement (--) operator in JavaScript?

The decrement operator in JavaScript decreases an integer value by one. This operator is often utilized in loops, counters, and mathematical computations where a value has to be decreased sequentially.

Types of Decrement Operators

The decrement operator (--) can be used in two ways:

  • Post-decrement (x--): Returns the current value of the variable first, then decrements it.
  • Pre-decrement (--x): Decrements the value first, then returns the new value.

Syntax

x--;  // Post-decrement
--x;  // Pre-decrement

Pre-decrement Example

Pre-decrement decreases the value first, then returns the new value:

let a = 33;
console.log("Original value:", a);

// Pre-decrement: decrements first, then returns new value
let result1 = --a;
console.log("After --a:", result1);
console.log("Current value of a:", a);

// Another pre-decrement
let result2 = --a;
console.log("After second --a:", result2);
Original value: 33
After --a: 32
Current value of a: 32
After second --a: 31

Post-decrement Example

Post-decrement returns the current value first, then decrements:

let b = 33;
console.log("Original value:", b);

// Post-decrement: returns current value first, then decrements
let result1 = b--;
console.log("Value returned by b--:", result1);
console.log("Current value of b:", b);

// Another post-decrement
let result2 = b--;
console.log("Value returned by second b--:", result2);
console.log("Final value of b:", b);
Original value: 33
Value returned by b--: 33
Current value of b: 32
Value returned by second b--: 32
Final value of b: 31

Comparison

Operator Operation Return Value
--x Pre-decrement New value (after decrement)
x-- Post-decrement Original value (before decrement)

Common Use in Loops

The decrement operator is frequently used in loops:

// Countdown loop using post-decrement
let count = 5;
while (count > 0) {
    console.log("Count:", count--);
}
console.log("Final count:", count);
Count: 5
Count: 4
Count: 3
Count: 2
Count: 1
Final count: 0

Conclusion

The decrement operator in JavaScript is essential for reducing variables by 1. Understanding the difference between pre-decrement and post-decrement is crucial, as they return different values despite both decreasing the variable.

Alshifa Hasnain
Alshifa Hasnain

Converting Code to Clarity

Updated on: 2026-03-15T21:37:43+05:30

755 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements