Comparison Operators
Table of Contents + โ
In the previous lesson, we learned how to do math. Now letโs learn how to compare values using comparison operators.
โ๏ธ What are Comparison Operators?
Comparison operators compare two values and always return a boolean: true or false. You will use them constantly to make decisions in your code.
| Operator | Meaning | Example | Result |
=== | Equal (value and type) | 5 === 5 | true |
!== | Not equal (value and type) | 5 !== 3 | true |
> | Greater than | 5 > 3 | true |
< | Less than | 5 < 3 | false |
>= | Greater than or equal | 5 >= 5 | true |
<= | Less than or equal | 3 <= 5 | true |
Here they are in code:
const age = 18;
console.log(age >= 18); // trueconsole.log(age < 13); // falseconsole.log(age === 18); // trueLetโs walk through what each line does:
- We store
18in a constant calledage, so every comparison below tests that value. age >= 18asks whetherageis greater than or equal to18; since18equals18, it returnstrue.age < 13asks whetherageis less than13; since18is not smaller, it returnsfalse.age === 18asks whetherageis exactly equal to18in both value and type, which istrue.
๐ฐ === vs == (Strict vs Loose Equality)
This is one of the most important things to understand in JavaScript. There are two ways to check equality:
===(strict) compares value and type, with no conversion==(loose) converts the types to match before comparing, which can give surprising results
console.log(5 === 5); // true โ same value, same typeconsole.log(5 === "5"); // false โ number vs stringconsole.log(5 == "5"); // true โ == converts "5" to a number firstLetโs compare the three lines to see how strict and loose equality differ:
5 === 5istruebecause both sides are the number5, so value and type match.5 === "5"isfalsebecause===does no conversion: the left side is a number and the right side is a string, so the types do not match.5 == "5"istruebecause==first converts the string"5"into the number5, then compares; this hidden conversion is exactly what makes==surprising.
Always prefer ===
Use === and !== almost all the time. They are predictable and avoid the
hidden type conversion that makes == a common source of bugs.
๐ค Comparing Strings
Comparison operators also work on strings. Strings are compared character by character based on their alphabetical (and case) order.
console.log("apple" < "banana"); // true โ "a" comes before "b"console.log("Apple" === "apple"); // false โ capital A is differentLetโs see how string comparison plays out here:
"apple" < "banana"compares the first characters:"a"comes before"b"in order, so the result istrue."Apple" === "apple"isfalsebecause comparison is case-sensitive, and a capital"A"is treated as a different character from a lowercase"a".
โ ๏ธ Common Mistakes to Avoid
| Mistake | Problem | Solution |
Using = instead of === | = assigns a value, it does not compare | Use === to compare, = only to assign |
Using == to compare | Hidden type conversion gives surprising results | Use === for predictable comparisons |
| Ignoring letter case in strings | "Yes" === "yes" is false | Convert case first if needed, e.g. .toLowerCase() |
๐ง Try It Yourself!
- Create an
agevariable and print whether it is>= 18. - Compare
5 === "5"and5 == "5"and notice the difference. - Compare two strings with
<and see which comes first alphabetically. - Try
"YES" === "yes", then fix it using.toLowerCase().
๐งฉ What Youโve Learned
- โ
Comparison operators return
trueorfalse - โ
===checks value and type;==converts types first - โ
Always prefer
===and!==for predictable results - โ Comparisons work on strings too, and are case-sensitive
- โ
=assigns a value,===compares values
๐ Whatโs Next?
Now that you can compare values, we will learn how to combine several conditions using logical operators. Letโs continue to Logical Operators.