cf.completefrontendCode editorOpen lab
THE JAVASCRIPT FIELD GUIDE

Comparisons

Compare JavaScript values safely with strict equality, string ordering, careful null checks, and the right way to detect NaN.

By the end, you can
  • 01
    Choose safe equalityUse === by default and recognize what == converts.
  • 02
    Predict orderingCompare strings, numbers, and string-number pairs without guessing.
  • 03
    Handle the weird edgesTreat null, undefined, and NaN with deliberate checks.

Comparison asks a yes-or-no question

A comparison is an expression that answers with a boolean: true or false. You use comparisons when code needs to make a choice: is the password long enough, did the score reach 100, is the value missing, or should this item come before that one?

You already met values in the Data types, Numbers & math, Strings, and Booleans, null & undefined lessons. This lesson focuses on the operators that compare those values.

The short version

Use === by default. It asks, “same type and same value?” Use == only when you deliberately want its conversions, especially the common value == null shortcut for null or undefined.

Real-life analogyStrict equality is a careful bouncer

Imagine two guests at a club. The strict bouncer checks the ID photo and the kind of ID before letting someone in. The lenient bouncer is willing to translate nicknames and lookalikes first. That is the heart of === versus ==.

In real life: Photo on the ID
In JavaScript: The value itself: 5, "5", false, null
In real life: Kind of ID card
In JavaScript: The type: number, string, boolean, object-ish missing value
In real life: Careful bouncer checks both
In JavaScript: === checks type and value
In real life: Lenient bouncer translates first
In JavaScript: == may convert, so "0" == 0 is true

Where the analogy stops: A real bouncer uses judgment. JavaScript follows exact rules every time, even when those rules surprise you.

In this lesson you will compare values with a live equality matrix, step through string order, untangle null and undefined, and learn why NaN is the one value equality cannot find.

=== vs ==

INTERACTIVE

JavaScript has several equality-looking operators. The one you want most of the time is strict equality, written ===. It returns true only when both sides have the same type and the same value.

  • 5 === 5 is true: both are numbers, both are 5.
  • "5" === 5 is false: string on the left, number on the right.
  • false === 0 is false: boolean on the left, number on the right.

Loose equality, written ==, may convert one side first. You learned the idea of implicit conversion in the Type conversion lesson. Here you only need the warning label: == can make different-looking values match.

Equality matrix playground
The questionPop out in the code editor (opens in a new tab)JavaScript
const left = "0";const right = 0; console.log(left == right);console.log(left === right);console.log(Object.is(left, right));
Real resultsChange the menus
==true
===false
Object.isfalse
Try it yourself

A string is converted to a number for ==, so text like "0" can match the number 0.

Pick two real JavaScript values. The three answers come from JavaScript itself: loose equality, strict equality, and Object.is.

Try "0" against 0, then "" against false, then null against undefined. Those are real results, not guesses. The third column uses Object.is, which you met in Numbers & math and will study deeply in “Equality & sameness.”

A small == truth grid
left == right0"0"""falsenullundefinedNaN
0truetruetruetruefalsefalsefalse
"0"truetruefalsetruefalsefalsefalse
""truefalsetruetruefalsefalsefalse
falsetruetruetruetruefalsefalsefalse
nullfalsefalsefalsefalsetruetruefalse
undefinedfalsefalsefalsefalsetruetruefalse
NaNfalsefalsefalsefalsefalsefalsefalse
Rule of thumb

If two values should be the same kind of thing, use ===. If one came from a text input, convert it on purpose first: Number(input) === 5. Your future self can read that without remembering the full == algorithm.

Comparing strings

STEP THROUGH

The ordering operators <, >, <=, and >= also return booleans. With two numbers, they feel like math. With two strings, JavaScript compares text character by character using character codes.

Real-life analogyA string comparison is a code-point dictionary

Picture a dictionary that orders words by the number printed on each character. It is consistent, but it is not always how a person would alphabetize names.

In real life: Look at the first letter
In JavaScript: Compare the first character
In real life: If tied, move to the next letter
In JavaScript: Compare the next character
In real life: This dictionary has a strange shelf order
In JavaScript: Uppercase codes come before lowercase codes

Where the analogy stops: Human dictionaries consider language, accents, and locale. JavaScript's plain < does not. The future “Comparing & sorting text” lesson covers localeCompare and Intl.Collator.

Step through the examples. In string mode, "10" < "9" is true because JavaScript compares the first characters: "1" comes before "9". In number mode, 10 < 9 is false. In mixed mode, a string-number pair is converted to numbers before the relational comparison.

String ordering vs number ordering
Step 0 of 4Ready
Your turn: follow the blue line

Choose a mode, predict each true or false, then step through the real comparisons.

Running in
  1. script
Next: line 1
Click the blue line to take the next stepPop out in the code editor (opens in a new tab)JavaScript
console.log("Apple" < "apple");console.log("10" < "9");console.log("a".charCodeAt(0));
CallStoreChangeResultRun = next line. Ran = already executed.
Recent returnsNothing yet. Start with the blue line.
Which operands should the visible lines use?

Switch modes, then predict again. Changing the mode starts a fresh replay.

A guided replay recorded from real JavaScript calls, not an engine debugger. Step follows executed statements; Back reviews a snapshot. Reset starts a fresh run.
Plain string order is not natural sort

Values like "item10" and "item9" show the same surprise: plain string order sees 1 before 9. Natural sorting and language-aware sorting belong to the later “Comparing & sorting text” lesson.

null & undefined comparisons

STEP THROUGH

null means “intentionally empty.” undefined means “no value was provided or found.” They are different values, but loose equality gives them one special handshake: null == undefined is true.

The tricky part is that equality and relational comparisons do not share one simple rule. Watch null == 0, null >= 0, and undefined >= 0 carefully.

Step through null and undefined
Step 0 of 7Ready
Your turn: follow the blue line

These lines look similar, but equality and relational operators use different rules. Step slowly.

Running in
  1. script
Next: line 1
Click the blue line to take the next stepPop out in the code editor (opens in a new tab)JavaScript
console.log(null === undefined);console.log(null == 0);console.log(null >= 0);console.log(null > 0);console.log(undefined == 0);console.log(undefined >= 0);
CallStoreChangeResultRun = next line. Ran = already executed.
Recent returnsNothing yet. Start with the blue line.
A guided replay recorded from real JavaScript calls, not an engine debugger. Step follows executed statements; Back reviews a snapshot. Reset starts a fresh run.
Practical advice

Avoid comparing possibly missing values with <, >, <=, or >=. Check for missing first, then compare a real number: value != null && value >= 0 or an explicit if.

NaN never equals itself

STEP THROUGH

NaN stands for “Not-a-Number,” a special number value that means a numeric operation failed. The Numbers & math lesson introduced Number.isNaN. Here is why it matters: equality cannot detect NaN.

Real-life analogyNaN is a mystery box

If two boxes are both sealed and labeled “unknown,” you still do not know whether the contents match. JavaScript treats NaN that way for equality.

In real life: A sealed box labeled invalid
In JavaScript: A failed numeric result, NaN
In real life: Comparing two mystery boxes
In JavaScript: NaN === NaN is still false
In real life: Use a scanner made for that label
In JavaScript: Use Number.isNaN(value)

Where the analogy stops: NaN is still a JavaScript number value, not an object or a real box. The analogy only explains why ordinary equality is the wrong tool.

Why equality cannot find NaN
Step 0 of 6Ready
Your turn: follow the blue line

Treat NaN like a mystery box: equality cannot identify it. Step through the safe checks.

Running in
  1. script
Next: line 1
Click the blue line to take the next stepPop out in the code editor (opens in a new tab)JavaScript
console.log(NaN == NaN);console.log(Number.isNaN(NaN));console.log(Object.is(NaN, NaN));console.log([NaN].includes(NaN));console.log([NaN].indexOf(NaN));
CallStoreChangeResultRun = next line. Ran = already executed.
Recent returnsNothing yet. Start with the blue line.
A guided replay recorded from real JavaScript calls, not an engine debugger. Step follows executed statements; Back reviews a snapshot. Reset starts a fresh run.

The array examples tease a future arrays lesson. For now, remember the professional habit: when the question is “is this NaN?”, ask Number.isNaN(value).

Where you’ll use this

Comparisons show up anywhere your program chooses a path. In front-end code, the values often start as text: form fields, URL pieces, stored settings, and data from a server. Safe code makes the conversion obvious before comparing.

A safe form checkPop out in the code editor (opens in a new tab)JavaScript
const ageText = "18";const age = Number(ageText); if (Number.isNaN(age)) {  console.log("Please enter a number.");} else if (age >= 18) {  console.log("Adult ticket");} else {  console.log("Child ticket");}
Rules of thumb for comparisons
SituationPreferWhy
Same value, no surprisesa === bChecks type and value without conversion.
Text input should be a numberNumber(input) === expectedThe conversion is visible and testable.
Missing means null or undefinedvalue == null or two strict checksThis is the one intentional loose shortcut many teams allow.
Detect invalid numberNumber.isNaN(value)Equality cannot find NaN.
Human text sortingLater: localeCompare / Intl.CollatorPlain < uses character code order, not language rules.

This is also why linters often warn about ==. They are not saying the operator is useless. They are saying hidden conversion is a common bug factory.

Common misconceptions

“== and === are basically the same.”

They agree only when conversion does not change the story. "0" == 0 is true; "0" === 0 is false.

“= compares values.”

= assigns. It stores the right-hand value in the left-hand name. Equality uses === or ==.

“String order is the same as dictionary order.”

Plain JavaScript string comparison uses character codes. Uppercase letters come before lowercase ones, and "10" comes before "9".

“null >= 0 and null == 0 should match.”

They use different rules. Relational comparison converts null to 0; loose equality has special cases and says null == 0 is false.

“Use x === NaN to find invalid numbers.”

That comparison is always false. Use Number.isNaN(x).

True or false? Comparison habits
  • score === 10
  • Number(input) === 5
  • Number.isNaN(total)
  • value == null
  • input == 5
  • result === NaN
Try it yourself
0 of 6 correct

Sort each comparison habit. Every card explains why it belongs there.

Choose a category for every card. You can change an answer at any time; Reset clears them all.

Practice: comparisons

5 EXERCISES

Try each one before opening the hints. The exact outputs have been checked against real JavaScript.

Exercise 1 · Warm-upPredict four comparisons

Predict the four console outputs, in order.

Starter codePop out in the code editor (opens in a new tab)JavaScript
console.log("0" == 0);
console.log("0" === 0);
console.log(null == undefined);
console.log(NaN === NaN);

Answer, then press Check. Spacing and letter case don’t matter.

    Exercise 2 · PracticeFix the hidden type bug

    This code unlocks level 5 even though the input is text. Rewrite the idea so conversion is explicit and the comparison is strict.

    Starter codePop out in the code editor (opens in a new tab)JavaScript
    const entered = "5";
    
    if (entered == 5) {
      console.log("Level 5 unlocked");
    }

    Answer, then press Check. Spacing and letter case don’t matter.

      Exercise 3 · PracticeSort-order surprise

      Predict the three outputs. This is the surprise that leads to natural sorting later.

      Starter codePop out in the code editor (opens in a new tab)JavaScript
      console.log("item10" < "item9");
      console.log("10" < "9");
      console.log(10 < 9);

      Answer, then press Check. Spacing and letter case don’t matter.

        Exercise 4 · ChallengeWrite an isMissing check

        Write the return expression for a function that should be true only for null or undefined.

        Starter codePop out in the code editor (opens in a new tab)JavaScript
        function isMissing(value) {
          return value == null;
        }
        console.log(isMissing(null));
        console.log(isMissing(undefined));
        console.log(isMissing(0));

        Answer, then press Check. Spacing and letter case don’t matter.

          Exercise 5 · ChallengeDetect NaN correctly

          A conversion failed. What expression should you use to detect that the result is NaN?

          Starter codePop out in the code editor (opens in a new tab)JavaScript
          const value = Number("nope");
          console.log(Number.isNaN(value));

          Answer, then press Check. Spacing and letter case don’t matter.

            Quiz: check your understanding

            7 QUESTIONS

            Your score counts first tries. Read every explanation; the wrong choices are designed to catch common comparison bugs.

            Lesson quiz · 7 questionsScore: first tries count
            1. Question 1 of 7Which comparison should be your default when checking whether two values are equal?

              Choose an answer to see the explanation.

            2. Question 2 of 7What does loose equality print here?

              Read the code, then predictPop out in the code editor (opens in a new tab)JavaScript
              console.log("5" == 5);

              Choose an answer to see the explanation.

            3. Question 3 of 7What does uppercase string order print?

              Read the code, then predictPop out in the code editor (opens in a new tab)JavaScript
              console.log("Apple" < "apple");

              Choose an answer to see the explanation.

            4. Question 4 of 7What does null >= 0 print?

              Read the code, then predictPop out in the code editor (opens in a new tab)JavaScript
              console.log(null >= 0);

              Choose an answer to see the explanation.

            5. Question 5 of 7What does undefined >= 0 print?

              Read the code, then predictPop out in the code editor (opens in a new tab)JavaScript
              console.log(undefined >= 0);

              Choose an answer to see the explanation.

            6. Question 6 of 7Which expression correctly detects the value NaN?

              Choose an answer to see the explanation.

            7. Question 7 of 7What does includes print for NaN?

              Read the code, then predictPop out in the code editor (opens in a new tab)JavaScript
              console.log([NaN].includes(NaN));

              Choose an answer to see the explanation.

            Key takeaways

            • === is your default equality operator because it checks type and value.
            • == converts before comparing; use it only deliberately, especially for value == null.
            • String ordering uses character codes, so capitals and number-looking strings can surprise you.
            • Check possibly missing values before using relational operators like >=.
            • NaN never equals itself; use Number.isNaN(value).

            Remember the one-liner.
            Compare strictly by default, convert explicitly when needed, and use special tools for special values like null and NaN.

            Up next: Logical operators.

            CompleteFrontend Clear concepts. Working examples.