In the previous lesson and exercise, you asked JavaScript ordering questions about your hiking pack. Is the total under the limit? Is it at most 8 kg? Each comparison answered with a Boolean that you could store and return.
Some questions aren’t about which value is bigger, though. They’re about whether two values are the same. Does the campsite on your booking match the sign at the trailhead? Did a hiker type the right answer to a trail quiz? In this lesson, you’ll write comparisons that answer questions like these, predict their results, and explain why two values that look alike can still count as different.
Ask Whether Two Values Match With ===
To ask whether two values are the same, JavaScript uses the strict equality operator, written as three equals signs: ===. Like < and <=, it compares the value on its left with the value on its right and gives back a Boolean. Read a === b as “a is strictly equal to b.”
Start the Node REPL with node in the integrated terminal. Predict each result before you press Enter:
> 8 === 8
true
> 8 === 7.5
false
> "pine" === "pine"
true
> "pine" === "birch"
false
> true === true
true
The operator works the same way for numbers, strings, and Booleans. When both sides hold the same value, you get true, and otherwise you get false. Type the three equals signs with no spaces between them, just like the two characters of <=.
Store the Match in a Variable
Like any comparison, a strict equality check produces a value you can store. Say you booked a spot at Pine campsite, and you want your script to confirm that the sign at the trailhead points to the same one. Create a file named campsite.js in your scratch/ folder:
const bookedSite = "pine";
const signSite = "pine";
const isRightSite = bookedSite === signSite;
console.log(`Right campsite: ${isRightSite}`);
This file runs from the terminal, not the REPL. If you still have the REPL open, type .exit and press Enter to get back to the terminal prompt. Then run the file with node scratch/campsite.js:
Right campsite: true
JavaScript reads the two variables, compares their values, and stores the Boolean in isRightSite. Now change signSite to "birch", save, and run the script again. This time, it prints Right campsite: false because the two strings differ.
Ask the Opposite With !==
Sometimes the question you care about is whether two values differ. For example, a warning should appear when the sign points to a campsite other than the one you booked. For that, JavaScript has the strict inequality operator, !==. Read a !== b as “a is not strictly equal to b.”
It always gives the opposite of the answer you get from === for the same two values. To try it, start the REPL again by typing node at the terminal prompt. Predict these before you run them:
> "pine" !== "birch"
true
> "pine" !== "pine"
false
> 3 !== 4
true
Choose whichever operator matches the wording of your question, so the variable name and its value agree. A variable called isWrongSite should hold the result of bookedSite !== signSite, which is true exactly when the sites differ.
Match Text Exactly, Including Case and Spaces
Strict equality for strings means every character matches, in the same order. In the previous lesson, you saw that JavaScript stores text as UTF-16 code units, and that uppercase letters have different codes from lowercase ones. So to ===, a capital letter is a different character:
> "Pine" === "pine"
false
> "PINE-7" === "PINE-7"
true
A person would read "Pine" and "pine" as the same campsite. JavaScript doesn’t guess what you meant, so it reports that the two strings aren’t identical.
Spaces count, too. A space is a character like any letter, so a string with an extra space at the end is longer:
> "pine " === "pine"
false
> "pine" === "pine"
true
The trailing space is invisible when you print the string, which makes this one easy to miss. If a match you expected comes back false, look closely at both strings for capital letters and stray spaces.
Match the Type, Too
Text that looks like a number is still text. You saw this with "10" < "9" in the previous lesson, and with true versus "true" in the lesson before. Strict equality makes the difference especially clear. Predict these three results:
> 3 === 3
true
> 3 === "3"
false
> true === "true"
false
The middle line compares the number 3 with the string "3". They print the same way, but one is a number and the other is text. For ===, two values are equal only when they’re the same type and hold the same value, so the answer is false.
Keep in mind how this differs from ordering comparisons. When you wrote "10" < 9, JavaScript quietly turned the text into a number before comparing. Strict equality never converts anything. That’s what makes it strict, and it means a true result tells you the two values really are the same.
This matters for your trip. Picture a booking form that reports the number of nights as the text "3". A check like nights === 3 would say false even though the booking is for three nights. Later in this course, you’ll learn to convert text like "3" into the number 3 yourself, so that comparisons like this one work as intended.
To practice spotting type differences, click the button below to open a conversation with the mentor. The mentor shows you one comparison at a time, waits for your prediction and reason, and gives feedback before the next one.
Tell = Apart From ===
You’ve used a single equals sign since your very first scripts. It doesn’t ask a question at all. It takes the value on its right and stores it in the variable on its left.
The line from campsite.js uses = and ===:
const isRightSite = bookedSite === signSite;
Read it from the right. First, === compares bookedSite with signSite and produces a Boolean. Then = gives isRightSite that Boolean as its starting value. Because the line begins with const, this = is part of creating the variable. One equals sign stores, and three equals signs compare.
A single = without const or let in front works differently. It’s the assignment operator, and it replaces the value of a variable that already exists. Say you meant bookedSite === signSite but typed bookedSite = signSite. JavaScript then tries to store a new value in bookedSite instead of comparing two values.
Because bookedSite was declared with const, its value can’t be replaced, so JavaScript reports a TypeError that says Assignment to constant variable. That message is a helpful clue that a comparison lost two of its equals signs.
Check Your Understanding
Before you move on, predict the Boolean result of each comparison below and say in one sentence why you expect it:
"PINE-7" === "pine-7"7 === "7"2.5 + 1.5 === 4"tent" !== "tarp"false === "false"
Then type them into the REPL to check your predictions. You can also open the box below to compare:
> "PINE-7" === "pine-7"
false
> 7 === "7"
false
> 2.5 + 1.5 === 4
true
> "tent" !== "tarp"
true
> false === "false"
false
Uppercase and lowercase letters are different characters, so the first pair doesn’t match. In the second and last lines, a number or a Boolean meets a string, and === never treats different types as equal. JavaScript finishes the sum before comparing, so the third line asks whether 4 equals 4. The two gear names differ, so !== answers true.
Now write a comparison of your own. A trail quiz at the visitor center asks for the name of the highest peak, and the expected answer is "Hochkönig". Leave the REPL with .exit first. In a new script, store a hiker’s answer in a variable named guess and the expected answer in expected. Then store the result of comparing them in isCorrect and print it with a template literal.
Run the script with a matching guess, then try "hochkönig" and "Hochkönig " with a trailing space. When you’re ready, compare your script with this one:
const guess = "Hochkönig";
const expected = "Hochkönig";
const isCorrect = guess === expected;
console.log(`Correct answer: ${isCorrect}`);
Correct answer: true
With "hochkönig" or "Hochkönig " as the guess, the script prints Correct answer: false because a lowercase letter or an extra space makes a different string. If JavaScript reports Assignment to constant variable., look for a single equals sign where you meant to write ===.
In the next exercise, Match Codes and Answers, you’ll return the results of === and !== comparisons from a supplied wrapper, just as you returned <= checks in Check Weights Against a Limit.