JS-023

Beginner2 min~2 min read#decrement#--#operators
Decrement --
Decrease a value by one
Goal
Learn how to use the -- operator to decrease a variable's value.
Explanation
- The -- operator decreases a variable's value by exactly one.
- It is shorthand for x = x - 1.
- Decrement is often used to count lives, time, or number of attempts.
- After the operator runs, the variable holds the new value.
- It is one of JavaScript's basic operators.
Analogy
Imagine a game where you lose one life after each mistake. That is how the -- operator works.
Code
let lives = 3;
lives--;
console.log(lives);Result: 2
Common mistake
You cannot write 10--. The -- operator works only with variables.
Remember
- ✦ -- subtracts one.
- ✦ x-- means x = x - 1.
- ✦ Decrement works only with variables.
Quick Quiz
What value will lives have after lives-- if lives starts at 3?