JavaScript Comparisons
Problem Statement
We've talked about performing basic mathematical functions, and assigning values to variables, but how would we check to see if a value is what we're expecting? In addition to performing arithmetic and assigning value to variables, JavaScript has additional operators for comparing values. The value returned by a comparison is always true
or false
.
Objectives
Identify equality operators
Compare numbers with the relational operators
Identify equality operators
There are four equality operators built into JavaScript:
loose equality operator (
==
)strict equality operator (
===
)loose inequality operator (
!=
)strict inequality operator (
!==
)
When writing JavaScript, you strongly prefer the strict operators, as the loose operators will return true even if the data types aren't the same. A string '42' is not the same as an integer 42. As developers we want to ensure that not only are the values the same, but also the data types.
==
Loose Equality Operator
==
Loose Equality OperatorThe loose equality operator returns true
if two values are equal:
However, it will also return true
if it can perform a type conversion (e.g., changing the string '42'
into the number 42
) that makes the two values equal:
This is confusing and inaccurate! It makes no sense that the string '0'
is equal to the boolean false
or that null
and undefined
— two completely different data types — are equivalent.
You should prefer ===
for comparisons.
===
Strict Equality Operator
===
Strict Equality OperatorThe strict equality operator returns true
if two values are equal without performing type conversions. Even if the values on both sides of the operator look similar (e.g., '42' === 42
), the ===
operator will only return true
if the data types also match:
This is logical and accurate! Prefer ===
for comparisons.
!=
Loose Inequality Operator
!=
Loose Inequality OperatorThe loose inequality operator is the opposite of ==
. It returns true
if two values are not equal, performing type conversions as necessary:
You should prefer !==
for comparisons.
!==
Strict Inequality Operator
!==
Strict Inequality OperatorThe strict inequality operator returns true
if two values are not equal and does not perform type conversions:
You should prefer !==
for comparisons.
Compare numbers with the relational operators
There are four relational operators built in to JavaScript:
greater than (
>
),greater than or equals (
>=
)less than (
<
)less than or equals (
<=
)
These operators work in a very similar way to the equality operators:
However, beware of type conversion when comparing non-numbers against numbers. For instance, when a string is compared with a number, the JavaScript engine tries to convert the string to a number:
If the engine can't convert the string into a number, the comparison will always return false
:
Strings are compared with other strings lexicographically, meaning character-by-character from left-to-right. The following returns false
because the Unicode value of 8
, the first character in 88
, is less than the Unicode value of 9
.
Top Tip: Stick to comparing numerical values with the relational operators and you'll be golden.
Conclusion
JavaScript contains both equality and comparison operators that assist us in writing functional code. Make sure you're preferring the strict equality operators, and only comparing numerical values with the relational operators, and you'll avoid those annoying troubleshooting errors that can drive you crazy!
Resources
MDN
Last updated