Hello! In our last lesson, we explored how to combine multiple conditions using logical operators (&&, ||, !) and uncovered the powerful "short-circuiting" behavior that makes your code more efficient. This was all about building more complex questions for our program to answer.
Today, we'll continue on the theme of writing cleaner and more concise code by learning some common shortcuts. This lesson focuses on operators that modify a variable's value. You will learn how to use shorthand assignment operators (like += and -=) and the increment/decrement operators (++ and --). These are fundamental tools you will see in almost every JavaScript project, so mastering them is key to both writing and reading code effectively.
Writing Cleaner Code with Shorthand Assignment
Often in programming, you need to update a variable based on its own current value. For example, if you're keeping score in a game:
let score = 100;
score = score + 20; // A player earned 20 points
console.log(score); // 120
Notice how we had to write score on both sides of the = sign. This is perfectly fine, but there's a more compact way to do it.
Shorthand assignment operators combine an arithmetic operation with an assignment. The expression score = score + 20; can be rewritten as:
let score = 100;
score += 20; // This means the same as score = score + 20;
console.log(score); // 120
This pattern works for all the main arithmetic operators:
a += bis the same asa = a + ba -= bis the same asa = a - ba *= bis the same asa = a * ba /= bis the same asa = a / b
Let's watch a quick video that demonstrates these operators in action.
JavaScript ARITHMETIC OPERATORS in 8 minutes! ➕
The video 'JavaScript ARITHMETIC OPERATORS in 8 minutes!' by Bro Code provides a fast and clear demonstration of these shorthand operators.
Watch the segment from 03:21 to 04:48. The instructor calls these 'augmented assignment operators' and shows how they provide a shortcut for reassigning variables.
The Ultimate Shortcuts: Increment and Decrement
The single most common modification is adding or subtracting 1 from a variable. This is especially frequent in loops, which we will cover soon. Because this is so common, JavaScript provides an even shorter shortcut: the increment (++) and decrement (--) operators.
variable++is the same asvariable = variable + 1orvariable += 1.variable--is the same asvariable = variable - 1orvariable -= 1.
Here is a simple example:
let numberOfItems = 5;
numberOfItems++; // We added one item
console.log(numberOfItems); // 6
numberOfItems--; // We removed one item
console.log(numberOfItems); // 5

Deep Dive: Prefix vs. Postfix
Here's where we get into an important "under the hood" detail that trips up many beginners. The ++ and -- operators can be placed before or after the variable name, and this changes their behavior in a subtle but critical way.
- Postfix (
variable++): The operator is placed after the variable. - Prefix (
++variable): The operator is placed before the variable.
When you're just incrementing a variable on its own line, they do the same thing. But when you use them as part of a larger expression (like assigning the result to another variable), the difference becomes clear.
- Postfix (
x++) returns the variable's value first, and then increments it. - Prefix (
++x) increments the variable first, and then returns the new value.
Let's see this in code:
// Postfix example
let a = 10;
let b = a++; // Step 1: b is assigned the original value of a (10).
// Step 2: a is incremented to 11.
console.log(`a is ${a}, b is ${b}`); // a is 11, b is 10
// Prefix example
let c = 10;
let d = ++c; // Step 1: c is incremented to 11.
// Step 2: d is assigned the new value of c (11).
console.log(`c is ${c}, d is ${d}`); // c is 11, d is 11
This distinction is crucial for understanding code written by others. The following resources provide excellent explanations.
First, this image gives a perfect visual summary of the postfix operation.

Next, let's read a textual breakdown of this concept.
Beginner's Guide to JavaScript Operators - Part One
The 'Beginner's Guide to JavaScript Operators - Part One' article has a great section that explains this difference with clear examples.
Read the section titled 'JavaScript prefix and postfix'. It's located just after the first block of code examples. Focus on the two sub-headings: 'Using ++/-- After the Operand' and 'Using ++/-- Before the Operand' to solidify your understanding.
Finally, watch this video where the instructor walks through this exact scenario, which can help make the concept click.
JavaScript Programming Tutorial 15 - Increment, Decrement, and Assignment Operators
In 'JavaScript Programming Tutorial 15', Caleb Curry provides a very clear, step-by-step explanation of the difference between prefix and postfix.
Watch from 02:09 to 04:23. Pay close attention to how the values of newPizza and slicesOfPizza change depending on whether he uses ++slicesOfPizza or slicesOfPizza++.
As a best practice, while it's essential to understand this behavior, it's often better to write code that is more explicit and easier to read. If you find yourself wondering whether to use prefix or postfix in a complex line of code, consider breaking it into two separate lines for clarity.
Test your understanding!
This challenge combines what you just learned with the const keyword from a previous lesson. Look at the code below. Why doesn't it work? How would you fix it?
const numberVariable = 0;
numberVariable++;
numberVariable++;
numberVariable++;
console.log(numberVariable);
Show answer
The code doesn't work because numberVariable is declared with const, which stands for "constant". A constant variable cannot be reassigned after it's initialized. The increment operator (++) is shorthand for numberVariable = numberVariable + 1, which is an attempt to reassign the variable. This will throw a TypeError.
To fix it, you need to declare the variable with let, which allows its value to be changed.
Corrected Code:
let numberVariable = 0;
numberVariable++;
numberVariable++;
numberVariable++;
console.log(numberVariable); // This will correctly print 3
This is an excellent example of how different concepts in the language interact!
Conclusion
You've now added several powerful shortcuts to your JavaScript toolkit. While they may seem small, using them fluently will make your code more professional and easier to write.
Key Takeaways:
- Shorthand Assignment Operators: Use
+=,-=,*=, etc., to modify a variable's value based on its current value in a concise way. - Increment and Decrement: Use
++and--as ultra-compact shortcuts for adding or subtracting 1. - Prefix vs. Postfix: This is a critical distinction.
- Postfix (
x++) evaluates to the original value, then updates the variable. - Prefix (
++x) updates the variable first, then evaluates to the new value.
- Postfix (
- Readability is Key: While these shortcuts are powerful, always prioritize clear and understandable code.
Next Up:
We've now completed our tour of JavaScript's fundamental operators. You know how to do math, make comparisons, combine logic, and use shortcuts. In our next lesson, we'll start a new module on Control Flow. You'll learn how to use the if statement to make your programs execute different blocks of code based on the conditions you've learned to create. This is where your programs will start to make decisions and become truly dynamic.