JavaScript

JS Basics

JS Variables & Operators

JS Data Types & Conversion

JS Numbers & Math

JS Strings

JS Dates

JS Arrays

JS Control Flow

JS Loops & Iteration

JS Functions

JS Objects

JS Classes & Modules

JS Async Programming

JS Advanced

JS HTML DOM

JS BOM (Browser Object Model)

JS Web APIs

JS AJAX

JS JSON

JS Graphics & Charts

JavaScript

JS Basics

JS Variables & Operators

JS Data Types & Conversion

JS Numbers & Math

JS Strings

JS Dates

JS Arrays

JS Control Flow

JS Loops & Iteration

JS Functions

JS Objects

JS Classes & Modules

JS Async Programming

JS Advanced

JS HTML DOM

JS BOM (Browser Object Model)

JS Web APIs

JS AJAX

JS JSON

JS Graphics & Charts

JS Comparisons


📘 JavaScript Comparisons – Understanding Equality, Inequality, and Relational Checks

Comparison operators in JavaScript allow you to compare two values and return a Boolean: true or false.

They are widely used in:

  • if statements

  • while/for loops

  • Filtering data

  • Conditional rendering, etc.


🔹 Types of Comparison Operators

Operator Meaning Example
== Equal to (loose) 5 == "5" → true
=== Strictly equal to (type+value) 5 === "5" → false
!= Not equal to (loose) 5 != "5" → false
!== Strictly not equal (type+val) 5 !== "5" → true
> Greater than 10 > 5 → true
< Less than 3 < 7 → true
>= Greater than or equal 8 >= 8 → true
<= Less than or equal 4 <= 3 → false

🔹 == vs ===

5 == "5";   // true (type coercion)
5 === "5";  // false (strict comparison)

✅ Use === for safer, more predictable comparisons.


🔹 Boolean Result

Every comparison returns true or false:

let a = 10;
let b = 20;

console.log(a > b);    // false
console.log(a <= b);   // true

🔹 Comparing Strings

console.log("apple" < "banana");  // true

🔤 Alphabetical comparison is based on UTF-16 character code order.


🔹 Comparing Different Types

JavaScript may convert types automatically in loose comparisons:

0 == false;      // true
0 === false;     // false
"5" == 5;        // true
null == undefined; // true
null === undefined; // false

🔹 Comparisons with Objects/Arrays

[] == false;        // true
{} == false;        // false
[] == [];           // false (different references)

⚠️ Comparing objects/arrays with == or === compares references, not content.


Practice Questions

Q1. What is the difference between == and === in JavaScript?

Q2. How do you compare whether two values are not equal and of different types?

Q3. What will 0 == false return and why?

Q4. How can you compare two numbers and check if one is greater or equal to the other?

Q5. Which operator would you use to check strict inequality?

Q6. How does JavaScript compare strings using < or > operators?

Q7. What is the result of "5" == 5 and why?

Q8. How do you compare whether a value is less than or equal to another in an if condition?

Q9. What happens when you compare two arrays like [1,2] === [1,2]?

Q10. Why is it recommended to use === over == in most cases?


JS Comparisons Quiz

Q1: What is the output of 5 == '5'?

A. false
B. true
C. error
D. undefined

Q2: Which operator checks both value and type?

A. ==
B. !=
C. ===
D. !===

Q3: What is the result of null == undefined?

A. true
B. false
C. error
D. NaN

Q4: Which operator represents strict inequality?

A. !=
B. ==!
C. !==
D. =!

Q5: What will "abc" < "bcd" return?

A. false
B. true
C. error
D. NaN

Q6: What is the output of 0 === false?

A. true
B. false
C. error
D. undefined

Q7: What does >= mean?

A. Less than or equal
B. Equal
C. Greater than or equal
D. Not equal

Q8: Which comparison returns false?

A. 10 > 5
B. 10 >= 10
C. "5" === 5
D. 5 != "5"

Q9: What is the result of [] == false?

A. true
B. false
C. error
D. undefined

Q10: What is the result of comparing NaN == NaN?

A. true
B. false
C. undefined
D. error

Go Back Top