JavaScript AcademyProfessional Engineering
Level 1 · FoundationsModule 3: Decisions and LogicLesson 11 of 150About 40 minutes

Lesson 11: Comparison operators

01Opening question

A login form compares the code you typed with the one that was sent to you. Should "1042" equal 1042?

Keep the question in mind. By the end of the lesson you will be able to answer it, and prove it with working code.

02Start with working code

=== compares value AND type. Convert the typed text first, then compare.

const typedCode = "1042";
const sentCode = 1042;
console.log(typedCode === sentCode);
console.log(Number(typedCode) === sentCode);

03Objective and prerequisites

Learning objective: Compare values with ===, !==, <, >, <= and >=, explain why === is preferred over ==, and compare strings correctly.

You need: Lesson 9: Booleans, null and undefined, Lesson 10: Type conversion and type checking

Skills practised: Logic, Comparison operators.

04Explanation

Comparison operators ask a question and answer with a boolean: > greater than, < less than, >= at least, <= at most, === equal, !== not equal.

=== (strict equality) is true only when both the value and the type match: 5 === 5 is true, "5" === 5 is false. Use === and !== by default.

== (loose equality) converts types before comparing, so "5" == 5 is true and 0 == "" is true too. These surprises cause bugs, which is why professionals avoid ==.

Strings are compared character by character: "apple" < "banana" is true. Capital letters come before lowercase ones ("Zebra" < "apple" is true), so convert case first when comparing user input.

05Definitions

Comparison operator
An operator that compares two values and gives true or false.
Strict equality (===)
True when two values have the same type and value.
Loose equality (==)
Converts types before comparing; can give surprising results.
!==
Strict not-equal.
Lexicographic order
Comparing text character by character, like a dictionary but by character code.

Open the full glossary

06Visual learning object

Equality comparison laboratory. Enter two values and compare ===, == and Object.is, with the type of each side.

Loading the interactive object…

Open the full Equality comparison laboratory page with its guided experiment.

07Examples

ex-011-01The comparison operators Browser-executable JavaScript

Each comparison produces true or false.

const total = 48;
console.log(total > 50);
console.log(total >= 48);
console.log(total < 100);
console.log(total === 48);
console.log(total !== 50);
Sample input
None
Expected output
false true true true true
Common mistake
Writing => instead of >=.
Try this
Check whether total is between 40 and 60 with two comparisons.
Source
examples/ex-011-01-comparison-operators.js (repository pending publication)

ex-011-02Strict versus loose equality Browser-executable JavaScript

== converts types first; === never does.

console.log("5" == 5);
console.log("5" === 5);
console.log(0 == "");
console.log(0 === "");
console.log(null == undefined);
console.log(null === undefined);
Sample input
None
Expected output
true false true false true false
Common mistake
Using == and accepting "" as equal to 0.
Try this
Compare true == 1 and true === 1.
Source
examples/ex-011-02-strict-vs-loose.js (repository pending publication)

ex-011-03Comparing strings Browser-executable JavaScript

Strings compare by character codes. Capitals sort before lowercase letters.

console.log("apple" < "banana");
console.log("Zebra" < "apple");
console.log("zebra" < "apple");
console.log("ADA".toLowerCase() === "ada");
console.log("10" < "9");
Sample input
None
Expected output
true true false true true
Common mistake
Comparing numbers stored as strings: "10" < "9" is true.
Try this
Compare the numbers 10 and 9 instead of the strings.
Source
examples/ex-011-03-string-comparison.js (repository pending publication)

ex-011-04Arrays compare by reference Browser-executable JavaScript

Two arrays with the same contents are still two different arrays.

const a = [1, 2];
const b = [1, 2];
const c = a;
console.log(a === b);
console.log(a === c);
console.log(a.length === b.length);
Sample input
None
Expected output
false true true
Common mistake
Expecting === to compare list contents.
Try this
Compare a[0] === b[0].
Source
examples/ex-011-04-compare-references.js (repository pending publication)

08Guided exercises

Three exercises: explore and modify, complete the implementation, then build independently. Tests check behaviour, so any correct solution passes.

Check the verification code

Convert typed text before a strict comparison.

const sentCode = 4821;
const typedCode = "4821";
const isCorrect = typedCode === sentCode;
console.log("Correct:", isCorrect);

Case-insensitive match

Normalise strings before comparing them.

const saved = "Ada.Lovelace@Example.com";
const typed = "  ada.lovelace@example.com ";
// TODO: compare the two emails ignoring case and surrounding spaces
const same = saved === typed;
console.log("Same email:", same);

Price rule checks

Express business rules as comparisons.

const price = 120;
const budget = 150;
const minimumOrder = 25;

09Prediction

Commit to an answer first, then run the real code to compare.

console.log(3 === 3.0);
console.log("3" === 3);
console.log("b" > "a");
console.log([3] === [3]);

What four lines are printed?

10Debugging investigation

A valid promo code is rejected

Shoppers who type save10 get "Invalid code" even though SAVE10 is a valid promotion.

const validCode = "SAVE10";
const typed = " save10 ";
const accepted = typed === validCode;
console.log(accepted ? "Code accepted" : "Invalid code");

More cases in the Debugging Detective Laboratory

11Practical scenario

Promo code check

A checkout compares the promo code a shopper typed with the valid code. Shoppers type with spaces and in any case, so the comparison must normalise both values and use ===.

const valid = "SAVE10";
const typed = " save10";
console.log(typed.trim().toUpperCase() === valid);

12Knowledge check

1. What does this print?

console.log("10" === 10);
  • true
  • false
  • undefined
  • An error

2. What does this print?

console.log(0 == "");
  • true
  • false

3. Which operator means "at least"?

  • <=
  • >=
  • =>
  • ==

4. What does this print?

console.log("100" < "9");
  • true
  • false

5. The check is always false for a correct password. Why?

const typed = "secret ";
console.log(typed === "secret");
  • Passwords cannot be compared
  • typed has a trailing space
  • === is case-insensitive
  • Strings need ==

6. Which is the professional default for equality?

  • ==
  • ===
  • Both are equivalent
  • Neither

13Solutions and tests

Each exercise's solution appears in the exercise once you have run its tests. Solutions are one possible answer: the tests accept any implementation with the same behaviour.

Automated acceptance tests for this lesson

These are the same checks the exercises run. They are visible on purpose: client-side tests help you learn and are not secret.

  • Check the verification code: Prints Correct: true; Still uses ===
  • Case-insensitive match: Prints Same email: true; Normalises both sides
  • Price rule checks: Prints three true values; The booleans are correct; Uses comparisons, not literals
  • ex-011-01: output matches the documented expected output
  • ex-011-02: output matches the documented expected output
  • ex-011-03: output matches the documented expected output
  • ex-011-04: output matches the documented expected output

Download the whole lesson folder (examples, exercises, solutions, tests) as ZIP

14Source files

The lesson lives in the companion repository at curriculum/level-01-foundations/module-03-decisions-and-logic/lesson-011-comparison-operators/. Every example on this page is generated from those files.

The GitHub repository (https://github.com/lsvekis/JavaScript-Professional-Academy) has not been published yet, so these links will work once it is. Until then, use the ZIP download above.

15Summary and next steps

  • Comparisons give booleans.
  • Use === and !==; == converts types and surprises.
  • Strings compare by character codes: normalise case and spaces.
  • Arrays and objects compare by reference.

16AI learning prompts

Copy a prompt into the AI assistant of your choice. Then verify what it says by running code here. No AI service is connected to this page.

Concept explanation === versus ==
Context: I am learning equality in JavaScript.
Task: Explain === and == with six examples, three of them surprising.
Constraints: Beginner level.
Expected behaviour: I can explain why === is preferred.
Testing: Give me four comparisons to predict.
Code prediction Comparison drill
Context: I know <, >, <=, >=, === and !==.
Task: Ask me ten comparison questions with numbers and strings.
Constraints: Wait for answers.
Expected behaviour: Each has one answer.
Testing: Explain string ordering for any I miss.
Debugging Comparison always false
Context: My check of a typed code against a saved code is always false.
Task: Help me find out why with questions about types and spaces.
Constraints: No direct answer at first.
Expected behaviour: I fix it myself.
Testing: Suggest logs that reveal the problem.
Exercise generation Business rules
Context: I finished the price rules exercise.
Task: Create two exercises that turn business rules into comparisons.
Constraints: Fictional data, exact output.
Expected behaviour: Each has three or more rules.
Testing: Include expected output.
Code review Equality review
Context: My script uses several == comparisons.
Task: Review which ones could misbehave and suggest replacements.
Constraints: Explain each risk briefly.
Expected behaviour: No == remains unless justified.
Testing: List the lines to change.