“Operators & Expressions for Web” – Learn how JavaScript thinks and calculates
Welcome back, young web coder! In Module One, you learned what operators and expressions are. You learned that operators are symbols that do actions, and expressions are pieces of code that produce values. You also learned how to use the browser console and how variables hold values.
Now it is time to roll up our sleeves and do math. In Module Two, we will learn about arithmetic operators (like +, -, *, /) and assignment operators (like =, +=, -=). These are the tools every website uses to count things, calculate prices, track scores, and more.
Think of arithmetic operators as your math tools. Assignment operators are like labels on boxes that store results. Together, they make websites smart. Let’s begin!
After finishing this module, you will be able to:
Ngozi is 14 years old and lives in Enugu. Her aunt runs a small restaurant. Every day, customers order plates of food, bottles of water, and soft drinks. Ngozi’s aunt wrote the prices on paper and added them by hand. Sometimes she made mistakes.
One day, Ngozi said, "Auntie, let me build you a simple bill calculator." She opened her browser console and typed:
let plate = 2500; let water = 200; let drink = 500; let total = (2 * plate) + (3 * water) + (1 * drink);
The console showed: 5600. Her aunt was amazed. Ngozi explained:
* multiplied the quantity by the price.+ added the subtotals.( ) grouped each item before adding.= stored the answer in total.Next, Ngozi added a discount:
let discount = 500; total -= discount; // total is now 5100
Her aunt was very happy. "This will save me so much time!" she said.
Ngozi smiled. "And I can even add more features later — like tax, tips, or percentages."
Moral of the story: Arithmetic and assignment operators are powerful tools for real businesses. They can calculate prices, discounts, and totals quickly and accurately.
Definition: Arithmetic operators are symbols that perform math operations on numbers.
Why it is important: They are the foundation of every calculation on the web.
Simple explanation: They are like the buttons on a calculator.
Real-life example: A bank adds interest with +.
School example: A teacher averages scores using + and /.
Home example: A family subtracts expenses using -.
Nigerian example: A trader multiplies price by quantity using *.
Illustration:
Arithmetic Operators: + addition - subtraction * multiplication / division % modulus (remainder) ** exponent (power)
Mini summary: Arithmetic operators do math on numbers.
Definition: The + operator adds two or more numbers together.
Why it is important: It is the most common operator in any program.
Simple explanation: + puts values together.
Real-life example: A bank adds deposits.
School example: A student adds scores.
Home example: A family adds grocery prices.
Nigerian example: A trader adds totals from two shops.
Illustration:
5 + 3 → 8 10 + 20 → 30 2.5 + 1.5 → 4
Mini summary: + adds numbers together.
Definition: The - operator subtracts one number from another.
Why it is important: It is used for balances, discounts, and differences.
Simple explanation: - takes away one value from another.
Real-life example: A bank subtracts withdrawals.
School example: A student finds how many points they lost.
Home example: A family subtracts savings from income.
Nigerian example: A trader subtracts expenses from sales.
Illustration:
10 - 4 → 6 50 - 25 → 25 5 - 8 → -3
Mini summary: - subtracts one number from another.
Definition: The * operator multiplies numbers together.
Why it is important: It is used to calculate totals with quantity and price.
Simple explanation: * repeats a number many times, quickly.
Real-life example: A bank calculates interest using multiplication.
School example: A student multiplies score by weight.
Home example: A family multiplies price by quantity.
Nigerian example: A trader multiplies quantity by unit price.
Illustration:
3 * 4 → 12 7 * 2 → 14 2.5 * 4 → 10
Mini summary: * multiplies numbers together.
Definition: The / operator divides one number by another.
Why it is important: It is used for averages, shares, and rates.
Simple explanation: / splits a number into equal parts.
Real-life example: A bank divides profit among shareholders.
School example: A student averages scores by dividing by number of subjects.
Home example: A family splits a bill among members.
Nigerian example: A trader divides the cost of a bag of rice to find the price per cup.
Illustration:
12 / 3 → 4 100 / 4 → 25 7 / 2 → 3.5
Mini summary: / divides one number by another.
Definition: The % operator gives the remainder after division.
Why it is important: It is used to check even/odd numbers and repeating cycles.
Simple explanation: % shows what is left over after dividing.
Real-life example: A bank checks if a number of transactions is even.
School example: A teacher divides students into groups of 4 and checks who is left over.
Home example: A family shares items equally and sees what is left.
Nigerian example: A trader packs eggs in cartons of 12 and checks the remainder.
Illustration:
10 % 3 → 1 (because 10 ÷ 3 = 3 remainder 1) 8 % 2 → 0 (even number) 7 % 2 → 1 (odd number)
Mini summary: % gives the remainder after division.
Definition: The ** operator raises a number to a power.
Why it is important: It is used for squaring, area, and growth calculations.
Simple explanation: ** multiplies a number by itself many times.
Real-life example: A bank calculates compound interest.
School example: A student calculates the area of a square.
Home example: A family calculates the area of a room.
Nigerian example: A trader calculates the area of farmland.
Illustration:
2 ** 3 → 8 (2 × 2 × 2) 5 ** 2 → 25 (5 × 5) 10 ** 0 → 1
Mini summary: ** raises a number to a power.
Definition: ++ adds 1 to a number. -- subtracts 1 from a number.
Why it is important: They are used in counters and loops.
Simple explanation: ++ is like saying "one more". -- is like saying "one less".
Real-life example: A bank counts transactions one by one.
School example: A teacher counts students entering the class.
Home example: A family counts how many eggs are used.
Nigerian example: A trader counts how many customers entered today.
Illustration:
let count = 0; count++; // count is now 1 count++; // count is now 2 count--; // count is now 1
Mini summary: ++ adds 1 and -- subtracts 1 from a number.
Definition: Assignment operators store values into variables.
Why it is important: Without them, you cannot save or reuse results.
Simple explanation: Assignment operators are like labeled boxes where you put values.
Real-life example: A bank assigns a value to "balance".
School example: A teacher assigns a value to "score".
Home example: A family assigns a value to "monthlyBudget".
Nigerian example: A trader assigns a value to "totalSales".
Illustration:
let balance = 5000; // assign balance = 6000; // reassign
Mini summary: Assignment operators store values into variables.
Definition: The = operator stores the value on the right into the variable on the left.
Why it is important: It is the most common assignment operator.
Simple explanation: = means "put this value into this box".
Real-life example: A bank sets balance to 5000.
School example: A student sets score to 90.
Home example: A family sets rent to 150000.
Nigerian example: A trader sets price to 3000.
Illustration:
let x = 5; let name = "Ada"; let isOpen = true;
Mini summary: = stores a value into a variable.
Definition: Compound assignment operators combine math and assignment in one step.
Why it is important: They save time and make code cleaner.
Simple explanation: Instead of writing x = x + 5, you write x += 5.
Real-life example: A bank adds interest to a balance with +=.
School example: A student adds bonus points with +=.
Home example: A family adds an expense with +=.
Nigerian example: A trader adds today’s sales with +=.
Illustration:
let x = 10; x += 5; // same as x = x + 5 → 15 x -= 3; // same as x = x - 3 → 12 x *= 2; // same as x = x * 2 → 24 x /= 4; // same as x = x / 4 → 6
Mini summary: Compound assignment combines math with assignment.
Definition: The %= operator assigns the remainder to a variable.
Why it is important: It is useful for cycles and remainders.
Simple explanation: It is like saying "put the remainder back into the box".
Real-life example: A bank uses %= to rotate transaction IDs.
School example: A teacher uses %= to pack students into groups.
Home example: A family uses %= to split bills evenly.
Nigerian example: A trader uses %= to know how many items will remain after packing.
Illustration:
let x = 17; x %= 5; // same as x = 17 % 5 → 2
Mini summary: %= assigns the remainder to a variable.
Definition: Precedence decides which operator runs first in an expression.
Why it is important: Wrong precedence gives wrong answers.
Simple explanation: JavaScript follows math rules: multiply before adding.
Real-life example: A bank multiplies interest before adding to balance.
School example: A student multiplies scores before averaging.
Home example: A family multiplies items before summing the bill.
Nigerian example: A trader multiplies quantity by price before adding other items.
Illustration:
2 + 3 * 4 → 14 (* first) (2 + 3) * 4 → 20 (parentheses first) 2 + 3 - 1 → 4 (left to right)
Step-by-step:
Mini summary: Precedence decides the order of calculation. Parentheses go first.
Definition: Mistakes happen. Knowing them helps you avoid them.
Why it is important: Small mistakes cause big errors.
Simple explanation: Like misreading a price at the market.
Real-life example: A bank charges wrong interest due to a wrong operator.
School example: A teacher misaverages due to missing parentheses.
Home example: A family miscounts a bill.
Nigerian example: A trader undercharges a customer.
Table of common mistakes:
| Mistake | What Happens | How to Fix |
|---|---|---|
| Forgetting parentheses | Wrong answer | Add ( ) |
| Using = instead of == | Assignment instead of check | Use === to compare |
| Dividing by zero | Infinity | Check denominator |
| Mixing numbers and strings | Wrong result | Convert types |
| Missing semicolon | Error | Add ; |
| Wrong variable name | ReferenceError | Check spelling |
Mini summary: Common mistakes: missing parentheses, wrong types, missing semicolons.
Definition: Best practices are good habits that make math code clean and safe.
Why it is important: Good habits prevent bugs and help others read your code.
Simple explanation: Like keeping your calculator clean.
Real-life example: A bank writes clean math code in its apps.
School example: A student writes neat calculations.
Home example: A family keeps a tidy budget sheet.
Nigerian example: A business writes simple, readable code.
List of best practices:
Mini summary: Best practices: parentheses, clean names, and testing.
You now know how to do math in JavaScript.
What you learned:
Next steps: In Module Three, you will learn about comparison, logical, and string operators.
Illustration:
Your Learning Journey:
Module 1: Foundations
|
V
Module 2: Arithmetic & Assignment
|
V
Module 3: Comparison & Logic
|
V
Module 4: Advanced & Certification
|
V
Web Operators Expert 🎉
Mini summary: You now have a full arithmetic toolkit. Time to compare and combine values!
| Word | Simple Definition |
|---|---|
| Arithmetic | Math with numbers. |
| Addition | Combining numbers with +. |
| Subtraction | Taking away with -. |
| Multiplication | Repeated addition with *. |
| Division | Splitting into equal parts with /. |
| Modulus | The remainder after division (%). |
| Exponent | Number raised to a power (**). |
| Increment | Add 1 to a number (++). |
| Decrement | Subtract 1 from a number (--). |
| Assignment | Storing a value in a variable (=). |
| Compound Assignment | Math plus assignment (+=, -=, etc.). |
| Precedence | Order of calculation. |
| Parentheses | ( ) group calculations. |
| Remainder | What is left after division. |
| Infinity | A result when dividing by zero. |
2 + 3 and press Enter.5.10 * 5 and press Enter.50.let x = 10;.x += 5;.x.15.10 % 3.1.8 % 2 to get 0.2 + 3 * 4 and see 14.(2 + 3) * 4 and see 20.let count = 0;.count++;.count and see 1.2.price * quantity for invoices.balance += interest to update accounts.(a + b + c) / 3 for averages.stock -= sold for remaining goods.amount %= 100 for split payments.20 - 5 for what is left.score += 10 for a new high score.price * 2 for two sweets.toys -= 1 for each toy given away.pagesRead += 10 every day.2 * 500 for two loaves of bread.income -= expenses for savings.hours + minutes / 60 for total hours.cups *= 2 to double a recipe.speed * time for distance.a = b = 5?-0 exists in JavaScript?+ add - subtract * multiply / divide % remainder ** power
x = x + 5 → x += 5 x = x - 3 → x -= 3 x = x * 2 → x *= 2 x = x / 4 → x /= 4
1. ( ) 2. ** 3. * / % 4. + -
10 % 3 10 ÷ 3 = 3 remainder 1 So 10 % 3 = 1
Module 1: Foundations
|
V
Module 2: Arithmetic & Assignment
|
V
Module 3: Comparison & Logic
|
V
Module 4: Advanced & Certification
|
V
Web Operators Expert 🎉
| Operator | Meaning | Example |
|---|---|---|
| + | Add | 5 + 3 = 8 |
| - | Subtract | 5 - 3 = 2 |
| * | Multiply | 5 * 3 = 15 |
| / | Divide | 10 / 2 = 5 |
| % | Remainder | 10 % 3 = 1 |
| ** | Power | 2 ** 3 = 8 |
| Operator | Meaning | Example |
|---|---|---|
| = | Assign | x = 5 |
| += | Add and assign | x += 5 |
| -= | Subtract and assign | x -= 5 |
| *= | Multiply and assign | x *= 5 |
| /= | Divide and assign | x /= 5 |
| %= | Remainder and assign | x %= 5 |
| Form | Meaning |
|---|---|
| x++ | Use x, then add 1 |
| ++x | Add 1, then use x |
| Expression | Value |
|---|---|
| 5 + 3 | 8 |
| 5 / 2 | 2.5 |
| 5 % 2 | 1 |
| 5 / 0 | Infinity |
| 0 / 0 | NaN |
Lesson 1: Arithmetic operators do math on numbers.
Lesson 2: + adds numbers.
Lesson 3: - subtracts numbers.
Lesson 4: * multiplies numbers.
Lesson 5: / divides numbers.
Lesson 6: % gives the remainder.
Lesson 7: ** raises to a power.
Lesson 8: ++ adds 1, -- subtracts 1.
Lesson 9: Assignment operators store values.
Lesson 10: = stores a value into a variable.
Lesson 11: Compound assignments combine math with assignment.
Lesson 12: %= assigns the remainder.
Lesson 13: Precedence controls calculation order.
Lesson 14: Common mistakes: missing parentheses, wrong types.
Lesson 15: Best practices: parentheses, clean names, testing.
Lesson 16: You now have a full arithmetic toolkit.
Congratulations! You have finished Module Two of the Operators & Expressions for Web course. You learned arithmetic operators (+, -, *, /, %, **) and assignment operators (=, +=, -=, *=, /=, %=). You learned about increment and decrement. You learned about precedence and parentheses. You learned common mistakes and best practices. Most importantly, you can now do math in JavaScript, which is the foundation of every website and app. In the next module, you will learn about comparison, logical, and string operators.
Match the operator to its meaning.
| Operator | Meaning |
|---|---|
| 1. + | A. Multiply |
| 2. - | B. Add |
| 3. * | C. Remainder |
| 4. % | D. Subtract |
| 5. ** | E. Power |
Answers: 1-B, 2-D, 3-A, 4-C, 5-E
Title: “Build a Simple Bill Calculator Together”
Instructions: In groups of 3–4, choose 3 items with prices and quantities. Write JavaScript expressions that calculate the total. Use at least one * and one +. Add a discount with -=. Print the results in the console. Present your code to the class.
Goal: Practice arithmetic and assignment operators.
Task: Open the browser console. Type at least 6 expressions:
Write down each expression and its result.
Project: “My First Bill Calculator”
Create a simple bill calculator in the console:
Example:
let price1 = 2500, qty1 = 2; let price2 = 200, qty2 = 3; let price3 = 500, qty3 = 1; let total = (price1 * qty1) + (price2 * qty2) + (price3 * qty3); total -= 500; // discount console.log(total); // 5100
Assignment: Ask a family member who sells goods how they price items. Then write JavaScript code that:
Submit: Your code and a short explanation.
In Module Three, we will learn about comparison, logical, and string operators. We will cover:
To prepare, complete the practical assignment and review the key vocabulary. Practice a few expressions in the browser console. Bring your curiosity!
See you in Module Three!
End of Module Two – Operators & Expressions for Web