Hiring Partners
Segilola Resources Operating LimitedTaviatech TechnologiesFCMBMTN NigeriaAdvans La Fayette Microfinance BankAboki AfricaFirst Bank of NigeriaOPay
← Operators and Expressions for Web · Lesson 4 of 5

Comparison, Logical and String Operators – Making Decisions on the Web

📖 Every lesson in this course is free to read right here, no account needed. Create a free account to track your progress, take the exam, and earn your certificate.
4
Operators & Expressions for Web – Module Three

Module Three: Comparison, Logical and String Operators – Making Decisions on the Web

“Operators & Expressions for Web” – Learn how JavaScript thinks and calculates

Module Introduction

Welcome back, young web coder! In Module One, you learned the basics of operators and expressions. In Module Two, you learned arithmetic and assignment operators.

Now it is time to make JavaScript think. In this module, we will learn about comparison operators (like == and >), logical operators (like && and ||), and string operators (like + for joining text).

These operators are what make a website smart. They help a login page check if a password is correct. They help a shop page show "In Stock" only when items are available. They help a game decide if the player has won.

By the end of this module, you will be able to write JavaScript that compares, decides, and reacts — just like real web apps do.

Let’s begin!

Learning Objectives

After finishing this module, you will be able to:

  • Explain what comparison operators do.
  • Use ==, ===, !=, !==, >, <, >=, <=.
  • Understand the difference between == and ===.
  • Explain what logical operators do.
  • Use &&, ||, and ! in expressions.
  • Understand truthy and falsy values.
  • Join strings with the + operator.
  • Use template literals for cleaner strings.
  • Give Nigerian examples of comparisons and logic.
  • Complete a mini project and practical assignment.

Warm-up Story: Chidi’s Login Page

Chidi is 14 years old and lives in Port Harcourt. He built a simple login page for his school’s coding club. The page asked for a username and password.

At first, Chidi had a problem. His code accepted every password. Anyone could log in! He said, "I need my page to compare what the user typed to the real password."

His teacher said, "Use the strict equality operator ===."

Chidi wrote:

let entered = "coding4life";
let real    = "coding4life";

if (entered === real) {
  console.log("Welcome!");
} else {
  console.log("Wrong password");
}
  

The console printed: Welcome!. Chidi was very happy.

Next, he wanted the page to welcome "Admin" or "Member" based on the username. He used the logical OR operator:

let username = "ada";
if (username === "ada" || username === "admin") {
  console.log("Hello, leader!");
}
  

Finally, he wanted to show a greeting. He used string concatenation:

let greeting = "Hello, " + username + "!";
console.log(greeting);
  

His login page became smarter. It could compare, decide, and respond. Chidi learned that comparison and logical operators are the brains of any website.

Moral of the story: Comparison operators check. Logical operators combine checks. String operators join words. Together, they make websites smart.

Main Lessons

Lesson 1: What are Comparison Operators?

Definition: Comparison operators compare two values and return true or false.

Why it is important: They let websites make decisions.

Simple explanation: They answer questions like "Is this bigger?" or "Are these the same?"

Real-life example: A bank checks if a balance is above zero.

School example: A teacher checks if a score is above pass mark.

Home example: A family checks if the shopping total is within budget.

Nigerian example: A trader checks if stock is below reorder level.

Illustration:

  Comparison operators return:

  true   or   false
  

Mini summary: Comparison operators check values and give true or false.

Lesson 2: The Equality Operator (==)

Definition: The == operator checks if two values are equal after converting them to the same type.

Why it is important: It is an easy way to compare values quickly.

Simple explanation: == asks "Are these the same?" but it is very forgiving.

Real-life example: A bank checks if a typed PIN equals a stored PIN.

School example: A teacher checks if a score equals 100.

Home example: A family checks if two shopping lists match.

Nigerian example: A trader checks if two prices are the same.

Illustration:

  5 == 5       →  true
  "5" == 5     →  true   (type conversion!)
  true == 1    →  true
  

Mini summary: == checks equality but converts types first.

Lesson 3: Strict Equality (===)

Definition: The === operator checks if two values are equal AND have the same type.

Why it is important: It is safer and prevents surprises.

Simple explanation: === asks "Are these the same value and the same type?"

Real-life example: A bank checks if a typed PIN (a number) strictly equals the stored PIN.

School example: A teacher checks if a score (a number) strictly equals a target score.

Home example: A family checks if the quantity (a number) strictly equals 5.

Nigerian example: A trader checks if the price (a number) strictly equals ₦3000.

Illustration:

  5 === 5      →  true
  "5" === 5    →  false
  5 === "5"    →  false
  

Mini summary: === checks equality and type. Always prefer ===.

Lesson 4: Not Equal (!= and !==)

Definition: != checks if two values are not equal. !== checks if they are not equal OR not the same type.

Why it is important: Used to reject wrong values.

Simple explanation: They are the "opposite" of equality checks.

Real-life example: A bank rejects a wrong PIN.

School example: A teacher rejects a wrong answer.

Home example: A family rejects a wrong shopping item.

Nigerian example: A trader rejects a wrong price.

Illustration:

  5 != 3       →  true
  5 != 5       →  false
  "5" !== 5    →  true
  

Mini summary: != and !== check if values are different. !== is stricter.

Lesson 5: Greater Than and Less Than (>, <)

Definition: > checks if the left value is greater. < checks if it is less.

Why it is important: Used to compare numbers.

Simple explanation: They are the "bigger" and "smaller" checks.

Real-life example: A bank checks if a withdrawal is greater than the balance.

School example: A teacher checks if a score is greater than 50.

Home example: A family checks if the bill is less than the budget.

Nigerian example: A trader checks if stock is less than 10.

Illustration:

  10 > 5    →  true
  10 < 5    →  false
  7 > 7     →  false
  

Mini summary: > and < check bigger and smaller.

Lesson 6: Greater/Less Than or Equal (>=, <=)

Definition: >= checks if the left value is greater OR equal. <= checks if it is less OR equal.

Why it is important: Very common in form validation and grading.

Simple explanation: Same as > and <, but includes "equal to".

Real-life example: A bank allows withdrawals up to the balance (<=).

School example: A teacher passes anyone with score >= 50.

Home example: A family keeps spending <= income.

Nigerian example: A trader sells if quantity <= stock.

Illustration:

  10 >= 10   →  true
  10 >= 9    →  true
  5 <= 5     →  true
  5 <= 3     →  false
  

Mini summary: >= and <= include the equal case.

Lesson 7: What are Logical Operators?

Definition: Logical operators combine multiple true/false checks into one.

Why it is important: Real life has more than one condition.

Simple explanation: They are the "and", "or", and "not" of code.

Real-life example: A bank requires you to have an account AND a valid ID.

School example: You can attend the trip if you paid AND brought a permission slip.

Home example: You can go out if you finished chores AND did homework.

Nigerian example: You can buy stock if you have money AND the price is good.

Illustration:

  &&   AND  (both true)
  ||   OR   (at least one true)
  !    NOT  (flips true to false)
  

Mini summary: Logical operators combine conditions.

Lesson 8: Logical AND (&&)

Definition: && returns true only if both sides are true.

Why it is important: It enforces strict conditions.

Simple explanation: && is like saying "you must do both".

Real-life example: A bank checks if you have enough money AND a valid account.

School example: A teacher passes you only if you attended AND submitted all work.

Home example: You can watch TV if you cleaned AND did homework.

Nigerian example: A trader accepts an order if stock is available AND payment is confirmed.

Illustration:

  true  && true   →  true
  true  && false  →  false
  false && true   →  false
  false && false  →  false
  

Mini summary: && needs both sides to be true.

Lesson 9: Logical OR (||)

Definition: || returns true if at least one side is true.

Why it is important: It gives flexibility.

Simple explanation: || is like saying "either this or that is fine".

Real-life example: A bank accepts a phone number OR an email for contact.

School example: You can submit by email OR by hand.

Home example: You can shop at Market A OR Market B.

Nigerian example: A trader accepts payment by transfer OR cash.

Illustration:

  true  || true   →  true
  true  || false  →  true
  false || true   →  true
  false || false  →  false
  

Mini summary: || is true if at least one side is true.

Lesson 10: Logical NOT (!)

Definition: ! flips a true to false or a false to true.

Why it is important: Used to invert checks.

Simple explanation: ! is like saying "the opposite".

Real-life example: A bank shows "Not available" if a service is !available.

School example: A teacher marks a student absent if !present.

Home example: A family stays home if !sunny.

Nigerian example: A trader does not sell if !inStock.

Illustration:

  !true    →  false
  !false   →  true
  

Mini summary: ! flips true and false.

Lesson 11: Truthy and Falsy Values

Definition: Truthy values act like true in logical checks. Falsy values act like false.

Why it is important: They affect how && , ||, and if statements behave.

Simple explanation: Some values "feel" like true or false to JavaScript.

Real-life example: A bank treats an empty balance (0) as a "no" for a loan.

School example: An empty string ("") is treated as no answer.

Home example: An empty shopping list is treated as "nothing to buy".

Nigerian example: A trader treats null prices as "no price".

Illustration:

  Falsy values:
  false, 0, "", null, undefined, NaN

  Truthy values:
  "text", 1, -1, [], {}
  

Mini summary: Some values act like false, others act like true. Learn the list.

Lesson 12: String Operators – Joining Text

Definition: The + operator also joins strings together.

Why it is important: Websites show messages like "Welcome, Ada".

Simple explanation: + glues words together.

Real-life example: A bank says "Dear " + customer + "."

School example: A teacher greets students: "Hello, " + name.

Home example: A family writes "Happy birthday, " + name.

Nigerian example: A trader sends "Your order of " + qty + " items is ready."

Illustration:

  "Hello, " + "Ada"     →  "Hello, Ada"
  "Total: " + 5000      →  "Total: 5000"
  

Mini summary: The + operator joins strings.

Lesson 13: Template Literals (Backticks)

Definition: Template literals use backticks (`) to make joining strings easier.

Why it is important: They are cleaner than using + many times.

Simple explanation: They let you put values inside a sentence directly.

Real-life example: A bank writes: `Dear ${name}, your balance is ${balance}`.

School example: A teacher writes: `${name} scored ${score}`.

Home example: A family writes: `We have ${count} eggs left.`

Nigerian example: A trader writes: `Your order costs ₦${price}.`

Illustration:

  let name = "Ada";
  let score = 90;

  `Hello, ${name}! Your score is ${score}.`
  

Mini summary: Template literals use backticks and ${ } for clean strings.

Lesson 14: Common Mistakes with Comparisons and Logic

Definition: Mistakes happen. Knowing them helps you avoid them.

Why it is important: Wrong comparisons give wrong decisions.

Simple explanation: Like using the wrong key on a lock.

Real-life example: A bank using == instead of === can accept "5" as 5.

School example: A teacher using = instead of === gives the wrong grade.

Home example: A family using || instead of && accepts the wrong list.

Nigerian example: A trader using < instead of <= misses a sale.

Table of common mistakes:

MistakeWhat HappensHow to Fix
Using == instead of ===Wrong type acceptedUse ===
Using = instead of ===Assigns valueUse === in conditions
Confusing && and ||Wrong logicRead conditions aloud
Not checking truthy/falsyWrong branchTest with console
Forgetting backticksString errorUse ` ` for template literals
Missing $ before { }Wrong outputWrite ${value}

Mini summary: Common mistakes: == vs ===, && vs ||, and forgotten backticks.

Lesson 15: Best Practices for Comparison and Logic

Definition: Best practices are good habits that make logic clear and safe.

Why it is important: Good habits prevent bugs and confusion.

Simple explanation: Like a clean desk — easier to think.

Real-life example: A bank writes clean conditions.

School example: A student writes clear if-statements.

Home example: A family writes clear shopping rules.

Nigerian example: A business writes clear code.

List of best practices:

  • Use === and !== instead of == and !=.
  • Name variables clearly.
  • Use parentheses to make logic clear.
  • Test in the console.
  • Use template literals for strings.
  • Avoid deep nesting of conditions.
  • Read your conditions aloud.
  • Use comments for tricky logic.
  • Check for truthy/falsy carefully.
  • Keep lines short.

Mini summary: Best practices: use ===, clear names, and test often.

Lesson 16: Putting It All Together – Your Logic Toolkit

You now know how to compare and combine values.

What you learned:

  • Comparison operators
  • == vs ===
  • != and !==
  • >, <, >=, <=
  • Logical operators &&, ||, !
  • Truthy and falsy values
  • String + operator
  • Template literals
  • Common mistakes and best practices

Next steps: In Module Four, you will learn advanced operators and complete your certification project.

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 logic toolkit. Time to learn advanced operators!

Key Vocabulary

WordSimple Definition
Comparison OperatorChecks two values and returns true/false.
Equality (==)Checks equal after type conversion.
Strict Equality (===)Checks equal value and type.
Not Equal (!=)Checks if values are different.
Strict Not Equal (!==)Checks if values or types differ.
Greater Than (>)Checks if left is bigger.
Less Than (<)Checks if left is smaller.
Greater Than or Equal (>=)Bigger or same.
Less Than or Equal (<=)Smaller or same.
Logical AND (&&)Both must be true.
Logical OR (||)At least one must be true.
Logical NOT (!)Flips true and false.
TruthyValues that act like true.
FalsyValues that act like false.
Template LiteralString using backticks and ${ }.

Important Concepts

  • Comparisons return true or false: They power decisions.
  • === is safer than ==: It checks type too.
  • !== is the opposite of ===: Strict not equal.
  • > and < compare size: >= and <= include equal.
  • && needs both: Strict condition.
  • || needs one: Flexible condition.
  • ! flips: The opposite check.
  • Truthy/falsy affect logic: Empty strings and 0 are falsy.
  • + joins strings: Concatenation.
  • Template literals are cleaner: Use backticks and ${ }.

Step-by-step Explanations

How to compare two numbers step by step

  1. Type 10 > 5 in the console.
  2. Press Enter.
  3. See true.
  4. Try 10 < 5 and see false.

How to check equality step by step

  1. Type 5 === 5 and see true.
  2. Type "5" === 5 and see false.
  3. Compare with "5" == 5 which gives true.

How to use && step by step

  1. Write two conditions: age >= 18 and hasID.
  2. Combine with &&: age >= 18 && hasID.
  3. Test with different values in the console.

How to use || step by step

  1. Write two conditions: hasEmail or hasPhone.
  2. Combine with ||: hasEmail || hasPhone.
  3. Test in the console.

How to use template literals step by step

  1. Write a backtick `.
  2. Type your message with ${value} inside.
  3. Close with a backtick `.
  4. Run in the console to see the result.

Real-life Examples

  • Banks: Compare balance to withdrawal.
  • Schools: Compare score to pass mark.
  • Homes: Compare spending to budget.
  • Shops: Compare stock to reorder level.
  • Web apps: Compare passwords and emails.

Nigerian Examples

  • Lagos banks: balance >= amount for transfers.
  • Abuja schools: score >= 50 to pass.
  • Port Harcourt shops: stock <= 10 to reorder.
  • Onitsha traders: price && quantity to check before selling.
  • Kano fintechs: amount > 0 && isVerified to send money.

Fun Examples Children Can Relate To

  • Games: score >= 100 means you win.
  • Snacks: money >= price means you can buy.
  • Toys: toys !== 0 means you still have toys.
  • Pets: isHungry && hasFood means feed the pet.
  • Reading: pages <= 10 means almost done.

Everyday Examples

  • Shopping: money >= total means you can pay.
  • Budget: expenses <= income means safe.
  • Time: hour < 12 means morning.
  • Cooking: water === 2 means add water.
  • Travel: distance > 100 means take the bus.

Parent Tips

  • Show how comparisons appear in real life.
  • Encourage testing with the console.
  • Explain === vs == in simple words.
  • Practice with shopping lists or budgets.
  • Praise curiosity and questions.
  • Read JavaScript articles together.
  • Keep sessions short.
  • Celebrate small wins.
  • Let them teach you.
  • Support their learning journey.

Interesting Facts

  • === was introduced to fix the confusion from ==.
  • NaN is not equal to itself (NaN === NaN is false!).
  • "" is falsy, "0" is truthy.
  • Template literals were added in 2015.
  • ! can be used twice: !!value gives true/false.
  • && returns the second value if the first is truthy.
  • || returns the first truthy value.
  • Nigerian fintechs use these operators in every transaction.

Did You Know?

  • Did you know 0 == "0" is true?
  • Did you know 0 === "0" is false?
  • Did you know [] == false is true?
  • Did you know null == undefined is true?
  • Did you know template literals can span multiple lines?
  • Did you know `${1+1}` prints 2?
  • Did you know !!"hello" is true?
  • Did you know 0 || 5 gives 5?

Remember This

  • Comparison operators give true/false.
  • === is safer than ==.
  • !== is stricter than !=.
  • > and < compare size.
  • >= and <= include equal.
  • && needs both true.
  • || needs at least one true.
  • ! flips true and false.
  • + joins strings.
  • Template literals are cleaner.

Common Mistakes

  • Using == instead of ===.
  • Using = instead of ===.
  • Confusing && and ||.
  • Forgetting truthy/falsy rules.
  • Forgetting backticks for template literals.
  • Missing $ before { }.
  • Not testing conditions in the console.
  • Deeply nested conditions.

Best Practices

  • Use === and !== instead of == and !=.
  • Name variables clearly.
  • Use parentheses to make logic clear.
  • Test in the console.
  • Use template literals for strings.
  • Avoid deep nesting.
  • Read conditions aloud.
  • Comment tricky logic.
  • Check truthy/falsy carefully.
  • Keep lines short.

Illustrations and Diagrams

Comparison Operators

  ===   equal value + type
  !==   not equal value + type
  >     greater than
  <     less than
  >=    greater or equal
  <=    less or equal
  

Logical AND vs OR

  A && B   → true only if both true
  A || B   → true if at least one true
  

Truthy vs Falsy

  Falsy:  false, 0, "", null, undefined, NaN
  Truthy: everything else
  

Template Literals

  `Hello, ${name}!`

  name = "Ada"
  → "Hello, Ada!"
  

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 🎉
  

Comparison Tables

== vs ===

Expression== result=== result
5 == 5truetrue
"5" == 5truefalse
0 == falsetruefalse
null == undefinedtruefalse

Logical Operators

ABA && BA || B
truetruetruetrue
truefalsefalsetrue
falsetruefalsetrue
falsefalsefalsefalse

Comparison Operators Summary

OperatorMeaningExample
===Equal value & type5 === 5
!==Not equal value & type5 !== "5"
>Greater than10 > 5
<Less than5 < 10
>=Greater or equal10 >= 10
<=Less or equal5 <= 5

String Join Methods

MethodExample
+ operator"Hi " + name
Template literal`Hi ${name}`

Lesson Summaries

Lesson 1: Comparison operators check values and return true/false.

Lesson 2: == checks equality with type conversion.

Lesson 3: === checks equality and type — safer.

Lesson 4: != and !== check if values differ.

Lesson 5: > and < compare bigger and smaller.

Lesson 6: >= and <= include equal.

Lesson 7: Logical operators combine conditions.

Lesson 8: && needs both sides true.

Lesson 9: || is true if at least one side is true.

Lesson 10: ! flips true and false.

Lesson 11: Truthy/falsy affect logical results.

Lesson 12: + joins strings.

Lesson 13: Template literals use backticks and ${ }.

Lesson 14: Common mistakes: == vs ===, && vs ||.

Lesson 15: Best practices: use ===, clear names, test often.

Lesson 16: You now have a full logic toolkit.

End-of-Module Summary

Congratulations! You have finished Module Three of the Operators & Expressions for Web course. You learned comparison operators (==, ===, !=, !==, >, <, >=, <=), logical operators (&&, ||, !), truthy and falsy values, and string operators (+ and template literals). You learned common mistakes and best practices. Most importantly, you can now write JavaScript that decides, reacts, and displays dynamic content. In the next module, you will learn advanced operators and complete your certification project.

Frequently Asked Questions

  1. What is a comparison operator? Checks two values and returns true or false.
  2. What is the difference between == and ===? == converts types; === does not.
  3. What is &&? Logical AND — both sides must be true.
  4. What is ||? Logical OR — at least one side is true.
  5. What is !? Logical NOT — flips true and false.
  6. What are truthy values? Values treated as true in logic.
  7. What are falsy values? Values treated as false: false, 0, "", null, undefined, NaN.
  8. How do I join strings? Use + or template literals.
  9. What is a template literal? A string using backticks and ${ }.
  10. Which equality should I use? Always prefer ===.

Matching Exercises

Match the operator to its meaning.

OperatorMeaning
1. ===A. Logical OR
2. !==B. Strict equality
3. &&C. Strict not equal
4. ||D. Greater than
5. >E. Logical AND

Answers: 1-B, 2-C, 3-E, 4-A, 5-D

Scenario-based Exercises

  1. Scenario: You want to check if two passwords are exactly equal.
    Answer: Use ===.
  2. Scenario: You want to check if a score is at least 50.
    Answer: Use >= 50.
  3. Scenario: You want a message if it is a weekday OR a holiday.
    Answer: Use ||.
  4. Scenario: You want to check if the user is NOT logged in.
    Answer: Use !isLoggedIn.
  5. Scenario: You want to greet the user by name.
    Answer: Use template literals.

Group Activity

Title: “Build a Login Check Together”

Instructions: In groups of 3–4, write JavaScript that checks a username and password against fixed values. Use === for both. Show a welcome message using template literals. Present to the class.

Goal: Practice comparisons, logic, and strings.

Individual Activity

Task: In the browser console, type at least 6 examples:

  • One == comparison.
  • One === comparison.
  • One >= comparison.
  • One && combination.
  • One || combination.
  • One template literal.

Write down each and its result.

Mini Project

Project: “My First Login Check”

Create a small login check in JavaScript:

  • Fix a username and password.
  • Ask the user for input (you can hard-code values).
  • Use === to compare.
  • Show "Welcome" or "Wrong" using template literals.
  • Add a special greeting for the admin.

Example:

  let user = "ada";
  let pass = "1234";

  let enteredUser = "ada";
  let enteredPass = "1234";

  if (enteredUser === user && enteredPass === pass) {
    console.log(`Welcome, ${user}!`);
  } else {
    console.log("Wrong login");
  }
  

Practical Assignment

Assignment: Ask a family member about a rule they use (e.g., shopping budget, chores). Then write JavaScript that:

  1. Checks a condition with === or >=.
  2. Combines two checks with && or ||.
  3. Shows a message using a template literal.
  4. Tests the code in the console.

Submit: Your code and a short explanation.

Key Takeaways

  • Comparison operators give true/false.
  • === is safer than ==.
  • !== is stricter than !=.
  • > and < compare size.
  • >= and <= include equal.
  • && needs both true.
  • || needs at least one true.
  • ! flips true and false.
  • + joins strings.
  • Template literals are cleaner.

Classroom Discussion Questions

  1. Why is === safer than ==?
  2. When would you use && instead of ||?
  3. What is the difference between != and !==?
  4. What does ! do to a value?
  5. What are truthy and falsy values?
  6. How do template literals make strings easier?
  7. What mistakes do beginners make with comparisons?
  8. How do comparisons help in real websites?
  9. What would you build with these operators?
  10. What did Chidi learn from building his login page?

Preparation for Module Four

In Module Four, we will learn advanced operators and complete the certification project. We will cover:

  • Ternary operator (? :)
  • Nullish coalescing (??)
  • Optional chaining (?.)
  • Spread (...) and rest (...)
  • typeof, in, instanceof
  • Bitwise operators
  • The certification project and presentation

To prepare, complete the practical assignment and review the key vocabulary. Practice a few comparisons in the console. Bring your curiosity!

See you in Module Four!


End of Module Three – Operators & Expressions for Web

🏆 Get Certified

🔒

Earn this certificate

Every lesson is already free to read. Sign up, finish the lessons, pass the exam (₦2,000 or 20,000 coins), and get a verified certificate with your name on it.

🎓 Sign Up to Get Certified
🛠️ Practice Tools
Hands-on simulators & labs - subscription required.
→
🎯 Internship Tasks
Real-world tasks to build your portfolio - unlock them with a subscription.
→