Comparison Operators0%

Comparison Operators

Beginner12 min read•Updated: Jul 11, 2026
Study Materials

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

OperatorMeaningExampleResult
===Strict Equality (Same value AND same type)5 === 5true
!==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 Than10 > 5true
<Less Than3 < 7true
>=Greater Than or Equal To10 >= 10true
<=Less Than or Equal To4 <= 9true

2. Strict Equality (===) vs. Loose Equality (==)

The Trap of Loose Equality (==):

When using ==, JavaScript uses complex implicit type conversion (coercion) before comparing:

JavaScript
console.log(5 == "5"); // true (String "5" converted to number 5)
console.log(0 == false); // true (false converted to 0)
console.log("" == false); // true (empty string converted to 0)
console.log(null == undefined);// true (special specification rule)
console.log([] == 0); // true (empty array coerced to empty string, then 0!)

The Safety of Strict Equality (===):

Strict equality never coerces types. If the types differ, it immediately returns false:

JavaScript
console.log(5 === "5"); // false (Number vs String)
console.log(0 === false); // false (Number vs Boolean)
console.log("" === false); // false (String vs Boolean)
console.log(null === undefined);// false (Null vs Undefined)
[!IMPORTANT] Universal Professional Rule: ALWAYS use === 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:

JavaScript
console.log("b" > "a"); // true (Unicode 98 > 97)
console.log("apple" < "banana"); // true ('a' comes before 'b')
console.log("10" < "2"); // true! (Lexicographically, '1' comes before '2'!)
Important Warning
[!WARNING] Always parse strings to numbers with 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!

JavaScript
const listA = [1, 2, 3];
const listB = [1, 2, 3];
 
console.log(listA === listB); // false! (They reside at different memory addresses in heap!)
 
const listC = listA;
console.log(listA === listC); // true! (They share the exact same memory reference!)

Interactive Quiz

Practice Quiz

Question 1
What does strict equality (===) check in JavaScript?
Question 2
What is the result of 0 == false vs 0 === false?
Question 3
Why does [10] === [10] evaluate to false?
Question 4
Which operator checks whether value $A$ is NOT strictly equal to value $B$?
Question 5
What is the output of "100" > "20"?
Next Lesson

Logical Operators

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Related Lessons

Previous LessonNext Lesson
Arithmetic OperatorsLogical Operators

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

PrevNext