JavaScript Roadmap — Day 8: Assignment Operators
Assignment Operators
Assignment operators are the workhorses of JavaScript — you use them in literally every program you will ever write. Master them completely today.
Look at how a simple shop tracking app updates its data every day:
💰 Daily sale recorded: totalSale = totalSale + 5000
📦 Stock decreased: stock = stock - 10
🎯 Discount applied: price = price * 0.9
These operations are so common that JavaScript created shortcut operators for them — so you never have to repeat the variable name twice. Instead of totalSale = totalSale + 5000 you just write totalSale += 5000.
This is not just a typing shortcut — it is the professional standard. Any senior developer who sees x = x + 1 immediately knows it was written by a beginner. After today you will write x++ or x += 1 — and your code will look professional.
1. What Are Assignment Operators?
An assignment operator stores a value into a variable. The most basic is = — but JavaScript has 10+ assignment operators that combine arithmetic and assignment into a single step.
| Operator | Meaning | Example | Same As |
|---|---|---|---|
= |
Assign | x = 5 |
— |
+= |
Add and assign | x += 3 |
x = x + 3 |
-= |
Subtract and assign | x -= 3 |
x = x - 3 |
*= |
Multiply and assign | x *= 2 |
x = x * 2 |
/= |
Divide and assign | x /= 2 |
x = x / 2 |
%= |
Remainder and assign | x %= 3 |
x = x % 3 |
**= |
Power and assign | x **= 2 |
x = x ** 2 |
2. The Basic = Operator — Just Assignment
= does one thing only — it stores the right side value into the left side variable. Do not confuse it with equality checking — that is the job of == and ===. The single = only stores.
🔥 Most Common Beginner Mistake: Writing if (x = 5) — this does not check equality, it assigns 5 to x and always returns true! Always write if (x === 5) inside conditions.
3. Compound Assignment Operators — The Professional Way 🔥
These operators combine arithmetic and assignment into a single step. This is not just shorter — it is the standard style in professional JavaScript. Every senior developer writes code this way:
4. Real World Use — Shop Inventory Tracker 🏪
Here is how these operators work in a real application — a shop inventory tracker for a clothing store in Faisalabad:
+= is not just for numbers — it concatenates strings too. When building HTML dynamically in a loop use html += "<li>" + item + "</li>". This is one of the most common patterns in real JavaScript applications.%= is checking even or odd — if (number % 2 === 0). If the remainder is 0 the number is even. You will use this in games, pagination, loops, and alternating row colors in tables.**= was added in ES2016. x **= 2 squares x. Use it for compound interest, area calculations, and scientific formulas. It is cleaner than Math.pow(x, 2) and reads more naturally in code.5. Destructuring Assignment — Modern JavaScript 🔥
Destructuring is an ES6 feature that lets you extract multiple values from an array or object in a single line. It is used everywhere in React, Node.js, and modern JavaScript. Once you learn it you will use it every single day:
6. Logical Assignment Operators — ES2021 ✨
These three operators were added in ES2021. They combine logical operators with assignment — and you will see them constantly in modern React and Node.js codebases:
7. Try It Yourself
Edit the code and click Run Code. Try the shop example with your own name and city!
8. Practice Tasks
Task 1 — Easy: Counter Challenge
Create a counter variable starting at 0. Use += to add 10 five times. Use -= to subtract 15 twice. Log the final value. Expected answer: 20.
Task 2 — Medium: Destructure Your Profile
Create a profile object with your name, age, city, and skills array. Use destructuring to extract all values. Then log a complete sentence: "I am [name], [age] years old from [city]. I know [skill1] and [skill2]."
Task 3 — Hard: Bank Account Simulator
Simulate a bank account starting at balance = 10000. Apply: deposit Rs5000 (+=), withdraw Rs3000 (-=), 2% monthly interest (*= 1.02), 1% bank charges (*= 0.99). Log balance after every step. Finally use ??= to set balance to 0 if it is null or undefined.