🔢 JavaScript Operators & Expressions
Estimated reading: 3 minutes 10 views

📘JavaScript Comparison Operators Explained with Examples

JavaScript comparison operators are essential for making decisions in your code. They allow you to compare values, determine equality, and control the program flow based on conditions. Mastering these operators is vital for writing robust logic, handling user input, and building dynamic applications.

  • 🔍 Compare numbers, strings, and objects
  • ✅ Control logic in if, else, and switch statements
  • ⚡️ Key for algorithms, validation, and data processing

🧠 Core Concepts & Theory

📌 What Are Comparison Operators?

Comparison operators in JavaScript evaluate two values and return a Boolean (true or false). They are used to compare variables, constants, or expressions.

Common Comparison Operators:

OperatorDescriptionExampleResult
==Equal to5 == '5'true
===Strict equal to5 === '5'false
!=Not equal to5 != '7'true
!==Strict not equal to5 !== '5'true
>Greater than7 > 5true
<Less than3 < 5true
>=Greater or equal5 >= 5true
<=Less or equal4 <= 3false

💡 Note:

  • == and != perform type coercion (loose comparison).
  • === and !== check both value and type (strict comparison).

💻 Code Implementation & Examples

// W3Office: Comparison Operator Examples
let a = 10;
let b = '10';

console.log(a == b); // ✅ true (loose equality)
console.log(a === b); // ✅ false (strict equality)
console.log(a != b); // ✅ false
console.log(a !== b); // ✅ true
console.log(a > 5); // ✅ true
console.log(a < 5); // ✅ false
console.log(a >= 10); // ✅ true
console.log(a <= 9); // ✅ false

Line-by-Line Breakdown:

  • a == b checks if values are equal after type conversion
  • a === b checks if values and types are both equal
  • a != b checks if values are not equal (after type conversion)
  • a !== b checks if values or types are not equal
  • a > 5 checks if a is greater than 5
  • a < 5 checks if a is less than 5
  • a >= 10 checks if a is greater than or equal to 10
  • a <= 9 checks if a is less than or equal to 9

🧩 Advanced Techniques & Best Practices

🧠 Best Practice: Always Use Strict Equality

// W3Office: Strict Comparison
let x = 0;
let y = false;

console.log(x == y); // ⚠️ true (not recommended)
console.log(x === y); // ✅ false (recommended)

💡 Tip:
Use === and !== to avoid unexpected type coercion.

⚠️ Common Pitfall: Comparing Objects

// W3Office: Object Comparison
let obj1 = { name: "W3Office" };
let obj2 = { name: "W3Office" };

console.log(obj1 == obj2); // ⚠️ false
console.log(obj1 === obj2); // ⚠️ false

📘 Note:
Objects are compared by reference, not by value.

📌 Summary & Takeaways

  • ✅ Comparison operators return Boolean values (true/false)
  • 🔄 Use === and !== for strict type-safe comparisons
  • ⚡ Avoid loose equality unless intentional
  • 🧩 Objects and arrays are compared by reference, not content

❓ FAQ Section

❓ What is the difference between == and === in JavaScript?
== checks for value equality with type conversion; === checks for both value and type equality (strict comparison).

❓ How do you compare two objects in JavaScript?
Direct comparison (== or ===) checks reference, not content. Use custom comparison or utility libraries to compare object values.

❓ Can you compare strings using comparison operators?
Yes, strings are compared lexicographically (alphabetical order) using <><=, and >=.

❓ What does !== do?
!== checks for both value and type inequality (strict not equal).

❓ Why should I avoid using == in JavaScript?
Because == allows type coercion, which can lead to unexpected results. Prefer === for predictable, type-safe comparisons.

Share Now :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

JavaScript — Comparison Operators

Or Copy Link

CONTENTS
Scroll to Top