JavaScript lesson · 18 min

JavaScript Equality and Comparison

Compare values with strict equality, greater-than checks, and clear boolean rules.

Code runs locally in your browser

What you will practice

  • Use strict equality with ===.
  • Understand why == can surprise you.
  • Combine comparisons into readable conditions.

What this means

Comparison means asking a question about values. Is this value equal to that value? Is this number larger? Is this string exactly the one we expect?

`===` is strict equality. It checks both value and type. For beginners, this is the comparison to use most of the time.

`==` allows JavaScript to convert types before comparing. That can be convenient, but it often creates surprising results.

If this is your first time seeing this

Comparison code asks questions and gets true/false answers.

Strict equality is your safest default because it does not quietly change types for you.

Mini glossary

Strict equality
`===`, which checks value and type.
Loose equality
`==`, which may convert types before comparing.
Comparison operator
A symbol such as `>`, `<`, `>=`, or `===` that compares values.

Example from everyday life

Strict equality is like checking both a ticket number and the ticket type. Loose equality is like letting someone rewrite the ticket format at the door. Sometimes it works, but it is easy to admit the wrong thing.

How it works step by step

  1. The code compares the number `5` with the string `"5"`.
  2. `points === required` is false because one value is a number and the other is a string.
  3. `points == required` is true because JavaScript converts before comparing.
  4. The final condition converts the string with `Number(required)` before using strict comparison.

Where you will use this

  • A route checks whether a status string is exactly `published`.
  • A form converts text input into a number before comparing scores.
  • A permissions check compares role names exactly to avoid accidental access.

Before you run the code

Prefer `===` and `!==` while learning. They make JavaScript do fewer hidden conversions.

If two values have different types, convert intentionally before comparing them.

Common beginner mistakes

  • Using `=` when you meant to compare. `=` assigns a value.
  • Using `==` and being surprised by type conversion.
  • Comparing a number from code with a string from a form without converting intentionally.

Run in browser

Try the example

Console output
Run the code to see console output here.
Exercise checks0 of 2 checks passed.
WaitingShows strict equality does not convert types
WaitingConverts intentionally before comparing

Code runs locally in a temporary browser worker with a timeout. It is not sent to Lumio analytics or executed on the server.

Try changing this next

  • Change `required` from `"5"` to `5`.
  • Change `status` to `draft`.
  • Print `typeof points` and `typeof required`.