- -- operator
- -= operator
- ++ operator
- += operator
- Accessing and setting content
- Array concat() method
- Array indexOf()
- Array length
- Array pop()
- Array shift
- Arrays
- Booleans
- Braces
- Callback function
- Calling the function
- Class
- Closure
- Code block
- Comment
- Conditions
- Console
- Constructor
- Creating a p element
- Data types
- Date getTime()
- Destructuring
- Else
- Else if
- Enum
- Equals operator
- Error Handling
- ES6
- Event loop
- Events
- Extend
- Fetch API
- Filter
- For loop
- forEach()
- Function
- Function bind()
- Function name
- Greater than
- Head element
- Hoisting
- If statement
- includes()
- Infinity property
- Iterator
- JSON
- Less than
- Local storage
- Map
- Methods
- Module
- Numbers
- Object.keys()
- Overriding methods
- Parameters
- Promises
- Random
- Reduce
- Regular expressions
- Removing an element
- Replace
- Scope
- Session storage
- Sort
- Splice
- String
- String concat()
- String indexOf()
- Substring
- Switch statement
- Template literals
- Ternary operator
- Title
- Type conversion
- While loop
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:
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.
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.
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.
let loadingSteps = 4;
loadingSteps--;
Examples of -- in JavaScript
Example 1: Postfix Decrement
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
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
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.
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
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:
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.
Sign up or download Mimo from the App Store or Google Play to enhance your programming skills and prepare for a career in tech.