“Operators & Expressions for Web” – Learn how JavaScript thinks and calculates
Welcome to the final module, young web coder! You have come a very long way. In Module One, you learned what operators and expressions are. In Module Two, you learned arithmetic and assignment operators. In Module Three, you learned comparison, logical, and string operators.
Now it is time to add the final layer: advanced operators and your certification project. You will learn about short, powerful operators that make your code cleaner and smarter: the ternary operator, nullish coalescing, optional chaining, and the spread and rest operators. You will also learn about typeof, in, and instanceof.
Think of this module as learning "power moves" in a video game. Once you know them, you can write less code and do more. At the end, you will build a real web project using everything you learned.
Let’s begin!
After finishing this module, you will be able to:
Tunde is 15 years old and lives in Ibadan. He was building a small website to help his teacher record exam scores. The website needed to show "Pass" or "Fail" based on a student’s score.
At first, he wrote it with a big if-else block:
if (score >= 50) {
result = "Pass";
} else {
result = "Fail";
}
His teacher said, "Can you make it shorter?" Tunde learned about the ternary operator. He rewrote it as:
result = score >= 50 ? "Pass" : "Fail";
His teacher smiled. "Much better!"
Next, Tunde wanted to show a message like "Hello [name]" or "Hello Guest" if no name was entered. He first used if-else. Then he learned about nullish coalescing:
message = "Hello " + (name ?? "Guest");
He also learned about optional chaining when his data came from an API. Sometimes a student might not have an email yet, so he wrote:
email = student?.contact?.email;
His code never broke again — even when some data was missing.
Finally, Tunde learned the spread operator to combine two lists of students into one:
allStudents = [...classA, ...classB];
At the end of the day, his small web app was clean, short, and worked beautifully. His teacher said, "You have become a real JavaScript expert!"
Tunde smiled. Advanced operators had made his code easier and smarter.
Moral of the story: Advanced operators let you do more with less code. They make your programs shorter, safer, and easier to read.
Definition: The ternary operator is a short way to write an if-else decision in one line.
Why it is important: It makes your code cleaner and shorter.
Simple explanation: It is like asking "Yes or No?" and giving two answers.
Real-life example: A bank shows "Adult" or "Minor" based on age.
School example: A teacher shows "Pass" or "Fail" based on score.
Home example: A family shows "Rainy" or "Sunny" based on weather.
Nigerian example: A trader shows "Buy" or "Wait" based on price.
Illustration:
condition ? valueIfTrue : valueIfFalse Example: score >= 50 ? "Pass" : "Fail"
Step-by-step:
Mini summary: The ternary operator is a short if-else in one line.
Definition: In real code, ternary operators often appear in templates, forms, and messages.
Why it is important: It keeps code readable when the choice is simple.
Simple explanation: It is like a shortcut in a video game.
Real-life example: A bank shows "Approved" or "Pending" on a loan.
School example: A teacher shows "Present" or "Absent".
Home example: A family shows "Open" or "Closed" for the shop.
Nigerian example: A trader shows "In Stock" or "Sold Out".
Illustration:
let status = isOpen ? "Open" : "Closed"; let greeting = hour < 12 ? "Good morning" : "Good afternoon";
Step-by-step:
Mini summary: Use ternary operators for short, simple decisions.
Definition: The nullish coalescing operator ( ?? ) gives a fallback value when the main value is null or undefined.
Why it is important: It prevents errors when data is missing.
Simple explanation: It is like saying, "If this is empty, use that instead."
Real-life example: A bank shows "Unknown" if a customer’s name is missing.
School example: A teacher shows "No score" if a student is missing.
Home example: A family shows "Guest" if no name is entered.
Nigerian example: A trader shows "No price" if a product has no price set.
Illustration:
let name = enteredName ?? "Guest"; let price = productPrice ?? 0;
Step-by-step:
Mini summary: The ?? operator gives a safe fallback for missing values.
Definition: The OR operator ( || ) also gives a fallback, but it triggers on any "falsy" value, not just null or undefined.
Why it is important: Using the wrong one can cause bugs, especially with 0 or "".
Simple explanation: || triggers on many "empty" things. ?? triggers only on truly missing things.
Real-life example: A bank shows "0" for a customer with zero balance, not "Not available".
School example: A teacher shows "0" for a score of zero, not "No score".
Home example: A family shows "0" for items sold, not "No items".
Nigerian example: A trader shows "₦0" for a free product, not "No price".
Illustration:
0 || "default" → "default" (wrong for numbers) 0 ?? "default" → 0 (correct)
Step-by-step:
Mini summary: Use ?? when 0 or empty strings are valid values.
Definition: Optional chaining ( ?. ) lets you safely access properties inside objects that might be missing.
Why it is important: It prevents your code from crashing.
Simple explanation: It is like asking, "Does this exist? If yes, continue. If no, stop."
Real-life example: A bank checks a customer’s phone before sending an SMS.
School example: A teacher checks a student’s email before sending notes.
Home example: A family checks a relative’s address before sending a card.
Nigerian example: A trader checks a product’s price before calculating total.
Illustration:
student?.contact?.email If student is undefined → returns undefined (no crash)
Step-by-step:
Mini summary: Optional chaining (?.) keeps your code safe when data is missing.
Definition: The spread operator ( ... ) "spreads" the items of an array or object into another.
Why it is important: It makes copying and combining easy.
Simple explanation: It is like pouring all the items from one bag into another bag.
Real-life example: A bank combines two customer lists.
School example: A teacher combines two class lists.
Home example: A family combines two shopping lists.
Nigerian example: A trader combines stock from two shops.
Illustration:
const all = [...classA, ...classB];
const person = { ...ada, city: "Enugu" };
Step-by-step:
Mini summary: The spread operator spreads values into new arrays or objects.
Definition: The rest operator ( ... ) collects many values into one array.
Why it is important: It lets functions take unlimited arguments.
Simple explanation: It is like gathering scattered toys into one box.
Real-life example: A bank accepts any number of deposits.
School example: A teacher collects scores for any number of tests.
Home example: A family collects any number of shopping items.
Nigerian example: A trader adds up any number of items to a cart.
Illustration:
function sum(...numbers) {
return numbers.reduce((a, b) => a + b, 0);
}
sum(1, 2, 3, 4); // 10
Step-by-step:
Mini summary: The rest operator collects values into an array.
Definition: typeof tells you what kind of value something is.
Why it is important: It helps you check data before using it.
Simple explanation: typeof is like asking "What is this?"
Real-life example: A bank checks if a value is a number before calculating interest.
School example: A teacher checks if a score is a number before grading.
Home example: A family checks if a price is a number before budgeting.
Nigerian example: A trader checks if a quantity is a number before selling.
Illustration:
typeof "Hello" → "string"
typeof 42 → "number"
typeof true → "boolean"
typeof {} → "object"
typeof [] → "object" (special case)
Mini summary: typeof tells you the data type of a value.
Definition: The in operator checks if a property exists in an object.
Why it is important: It helps you avoid missing data errors.
Simple explanation: It is like asking, "Is this key inside the box?"
Real-life example: A bank checks if a customer record has an email field.
School example: A teacher checks if a student record has a phone field.
Home example: A family checks if a recipe has a sugar field.
Nigerian example: A trader checks if a product has a price field.
Illustration:
"email" in customer → true or false "price" in product → true or false
Mini summary: The in operator checks if a property exists in an object.
Definition: instanceof checks if an object was created by a certain class or constructor.
Why it is important: It confirms what type of object you have.
Simple explanation: It is like asking, "Was this made by this factory?"
Real-life example: A bank checks if an object is a Date.
School example: A teacher checks if an object is a Student object.
Home example: A family checks if an object is a Recipe.
Nigerian example: A trader checks if an object is a Product.
Illustration:
new Date() instanceof Date → true
[] instanceof Array → true
{} instanceof Object → true
Mini summary: instanceof tells you what class an object came from.
Definition: Bitwise operators work on the binary form (1s and 0s) of numbers.
Why it is important: They are fast, but mostly used in advanced code.
Simple explanation: It is like working with the smallest pieces of a number.
Real-life example: A bank uses bitwise flags for account settings.
School example: A teacher uses flags to mark which students are present.
Home example: A family uses flags for chores done or not done.
Nigerian example: A trader uses flags for "paid or unpaid" orders.
Illustration:
5 & 3 → 1 5 | 3 → 7 5 ^ 3 → 6 ~5 → -6 5 << 1 → 10 5 >> 1 → 2
Mini summary: Bitwise operators work on the binary form of numbers. Beginner coders rarely need them.
Definition: You can combine operators to write short, powerful code.
Why it is important: It shows real-world coding skill.
Simple explanation: It is like using several power moves in a game.
Real-life example: A bank formats a greeting using ternary and ??.
School example: A teacher shows status using ternary and ?.
Home example: A family shows a note using ?? and ?.
Nigerian example: A trader shows a price using ?? and ?.
Illustration:
const display = user?.name ?? "Guest"; const status = score >= 50 ? "Pass" : "Fail"; const label = price > 0 ? "₦" + price : "Free";
Mini summary: Combining operators makes powerful, clean code.
Definition: Mistakes happen. Knowing them helps you avoid them.
Why it is important: Advanced operators can cause tricky bugs.
Simple explanation: Like pressing the wrong button on a game controller.
Real-life example: A bank uses || instead of ?? and shows "0" as "Not available".
School example: A teacher uses = instead of === and gets wrong results.
Home example: A family uses ?. incorrectly and hides real errors.
Nigerian example: A trader uses ?? when || was needed and misses a valid price.
Table of common mistakes:
| Mistake | What Happens | How to Fix |
|---|---|---|
| Using || instead of ?? | Zero and "" treated as missing | Use ?? for valid zeros |
| Using = instead of === | Assignment instead of comparison | Use === for comparison |
| Overusing ?. | Hides real bugs | Only use when data may be missing |
| Nested ternaries | Hard to read | Use if-else for complex logic |
| Forgetting () in spread | Syntax error | Use ... correctly |
| Confusing rest and spread | Wrong data | Remember context |
Mini summary: Common mistakes: using || instead of ??, overusing ?., and nesting ternaries.
Definition: Best practices are good habits that make your code clean and safe.
Why it is important: Good habits prevent bugs and make code easier for others to read.
Simple explanation: Like keeping your room clean — easier to find things.
Real-life example: A bank writes clean, safe code.
School example: A student writes neat, easy-to-read code.
Home example: A family keeps a shared budget sheet tidy.
Nigerian example: A business writes clean code that other developers can maintain.
List of best practices:
Mini summary: Best practices: use strict equality, ?? for zeros, and keep code readable.
Your certification project brings everything together.
Project idea: Build a small interactive web page using every kind of operator you learned.
Steps:
Illustration:
Project Flow:
Idea
|
V
Plan
|
V
HTML + CSS + JS
|
V
Test
|
V
Present 🎉
Mini summary: The certification project uses every operator you learned.
You have learned a lot. Let’s review.
Module One: What operators and expressions are.
Module Two: Arithmetic and assignment operators.
Module Three: Comparison, logical, and string operators.
Module Four: Advanced operators and certification project.
Illustration:
Your Skills: +----------------+ | Expressions | +----------------+ +----------------+ | Arithmetic | +----------------+ +----------------+ | Comparison | +----------------+ +----------------+ | Advanced | +----------------+ +----------------+ | Certification | +----------------+
Mini summary: You have mastered all levels of JavaScript operators.
Your certification is proof that you are a Web Operators Expert.
Next steps:
Illustration:
Certification
|
V
Portfolio
|
V
Share with others
|
V
Help others learn
|
V
Apply skills
|
V
Web Operators Expert 🎉
Mini summary: Your certification opens doors to more advanced JavaScript.
| Word | Simple Definition |
|---|---|
| Ternary | Short if-else in one line. |
| Nullish Coalescing (??) | Fallback for null or undefined. |
| Optional Chaining (?.) | Safe access to possibly missing data. |
| Spread (...) | Spreads values into a new array or object. |
| Rest (...) | Collects many values into one array. |
| typeof | Tells the data type of a value. |
| in | Checks if a property exists in an object. |
| instanceof | Checks the class of an object. |
| Bitwise | Works on binary digits (1s and 0s). |
| Falsy | Values treated as false in logic. |
| Truthy | Values treated as true in logic. |
| Bug | A mistake in code. |
| Strict Equality (===) | Checks value AND type. |
| Immutable | Cannot be changed. |
| Fallback | Backup value. |
score >= 50
/ \
Yes No
| |
"Pass" "Fail"
value ?? fallback value is null/undefined → use fallback otherwise → use value
user?.profile?.name
|
V
Returns undefined if any part is missing
Spread: [ ...a, ...b ] (out) Rest: (...values) (in)
Module 1: Foundations
|
V
Module 2: Arithmetic & Assignment
|
V
Module 3: Comparison & Logic
|
V
Module 4: Advanced & Certification
|
V
Web Operators Expert 🎉
| Value | ?? Result | || Result |
|---|---|---|
| 0 | 0 | Fallback |
| "" | "" | Fallback |
| null | Fallback | Fallback |
| undefined | Fallback | Fallback |
| Feature | Spread | Rest |
|---|---|---|
| Direction | Outward | Inward |
| Use | Copy/combine | Collect |
| Example | [...a, ...b] | function(...args) |
| Value | typeof |
|---|---|
| "hi" | string |
| 5 | number |
| true | boolean |
| [] | object |
| null | object |
| Operator | Use Case |
|---|---|
| ? | Quick if-else |
| ?? | Safe fallback |
| ?. | Safe access |
| ... | Spread / rest |
| in | Check property |
| instanceof | Check class |
Lesson 1: Ternary operator is a short if-else in one line.
Lesson 2: Ternary is best for simple true/false decisions.
Lesson 3: ?? gives a fallback for null or undefined.
Lesson 4: Use ?? instead of || for valid zeros and empty strings.
Lesson 5: ?. safely accesses possibly missing data.
Lesson 6: Spread copies and combines arrays and objects.
Lesson 7: Rest collects values into one array.
Lesson 8: typeof tells you the type of a value.
Lesson 9: in checks if a property exists in an object.
Lesson 10: instanceof checks the class of an object.
Lesson 11: Bitwise operators work on binary numbers.
Lesson 12: Combining operators makes short, powerful code.
Lesson 13: Common mistakes: || vs ??, too many ternaries.
Lesson 14: Best practices: strict equality, ?? for zeros, keep it readable.
Lesson 15: Certification project uses every operator.
Lesson 16: Review of all four modules.
Lesson 17: Certification opens doors to more JavaScript.
Congratulations! You have finished Module Four and the entire Operators & Expressions for Web course. You learned the ternary operator, nullish coalescing, optional chaining, spread, rest, typeof, in, instanceof, and even bitwise operators. You learned common mistakes and best practices. You prepared and completed your certification project. Most importantly, you are now a Web Operators Expert. Keep coding, keep learning, and keep building!
Match the operator to its meaning.
| Operator | Meaning |
|---|---|
| 1. ? : | A. Safe access to missing data |
| 2. ?? | B. Short if-else |
| 3. ?. | C. Fallback for null/undefined |
| 4. ... (in front) | D. Collect values |
| 5. ... (in function) | E. Spread values |
Answers: 1-B, 2-C, 3-A, 4-E, 5-D
Title: “Build a Mini App Together”
Instructions: In groups of 3–4, choose a small web idea (calculator, quiz, form). Plan which operators you will use. Write at least one example of ternary, ??, ?., spread, and rest. One person writes HTML, one writes JS, one tests, one presents. Share with the class.
Goal: Practice using advanced operators in a real project.
Task: Write short JavaScript code that uses:
Hint: Keep each example to 2–3 lines.
Project: “My First Advanced Operators Page”
Build a small web page with JavaScript that:
Example:
let score = 65;
console.log(score >= 50 ? "Pass" : "Fail"); // Pass
let user = { name: "Ada" };
console.log(user?.contact?.email ?? "No email");
let a = [1, 2], b = [3, 4];
console.log([...a, ...b]); // [1,2,3,4]
Assignment: Build the certification project — a small interactive web page that uses at least six different operators from the entire course. Include:
Write clean code with comments and present it to your class.
Submit: Your HTML, CSS, and JavaScript files with your presentation.
Congratulations! You have completed the entire Operators & Expressions for Web course. Here are some next steps you can take:
Remember, this is just the beginning. You are now a Web Operators Expert. Keep coding, keep learning, and keep building!
End of Module Four – Operators & Expressions for Web
🎉 Congratulations! You have completed the entire Operators & Expressions for Web course! 🎉