Comparison Operators
Comparison Operators: Strict vs. Loose Equality
Comparison operators test the relationship between two values and return a Boolean (true or false). Master the difference between loose equality (==) and strict equality (===) to eliminate subtle bugs.
1. Overview of Comparison Operators
| Operator | Meaning | Example | Result |
|---|---|---|---|
=== | Strict Equality (Same value AND same type) | 5 === 5 | true |
!== | Strict Inequality (Different value OR different type) | 5 !== "5" | true |
== | Loose Equality (Type coerces before comparison) | 5 == "5" | true (Caution!) |
!= | Loose Inequality (Type coerces before comparison) | 5 != "5" | false |
> | Greater Than | 10 > 5 | true |
< | Less Than | 3 < 7 | true |
>= | Greater Than or Equal To | 10 >= 10 | true |
<= | Less Than or Equal To | 4 <= 9 | true |
2. Strict Equality (===) vs. Loose Equality (==)
The Trap of Loose Equality (==):
When using ==, JavaScript uses complex implicit type conversion (coercion) before comparing:
The Safety of Strict Equality (===):
Strict equality never coerces types. If the types differ, it immediately returns false:
=== and !==. Never use == or !=. Every reputable company linter (ESLint, Airbnb, Google) enforces this rule strictly.3. Relational Comparisons with Strings
When comparing strings using >, <, >=, <=, JavaScript compares character codes alphabetically (lexicographically) using their ASCII/Unicode numerical values:
Number() or parseInt() before performing mathematical range comparisons!4. Comparing Objects and Arrays
Remember: Objects and Arrays are compared by memory reference, NOT by internal contents!
Practice Quiz
===) check in JavaScript?0 == false vs 0 === false?[10] === [10] evaluate to false?"100" > "20"?Logical Operators
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Related Lessons
| Previous Lesson | Next Lesson |
|---|---|
| Arithmetic Operators | Logical Operators |
Practice Quiz
Test your understanding of this lesson with 5 questions. Each question has one correct answer.