🧠 Booleans & Comparisons
Estimated reading: 4 minutes 47 views

⚖️ JavaScript — Comparisons: A Complete Guide to Equality, Identity, and Logical Decisions


🧲 Introduction — Why JavaScript Comparisons Matter

In JavaScript, comparison operations are at the heart of every decision your code makes. From validating user input to performing complex conditionals in control structures like if and switch, understanding how JavaScript compares values is critical for accurate logic and bug-free programs.

By the end of this guide, you’ll understand:

  • ✅ The difference between loose (==) and strict (===) equality
  • ✅ How type coercion affects comparisons
  • ✅ Relational operators like <, >, <=, >=
  • ✅ Object vs. primitive comparisons
  • ✅ Best practices and gotchas that can trip up even experienced devs

🔑 Comparison Operators in JavaScript

Here are the most commonly used JavaScript comparison operators:

OperatorNameDescription
==Loose equalityCompares values after type coercion
===Strict equalityCompares values and types
!=Loose inequalityNot equal, allows type conversion
!==Strict inequalityNot equal, no type conversion
<Less thanReturns true if left < right
<=Less than or equalReturns true if left ≤ right
>Greater thanReturns true if left > right
>=Greater than or equalReturns true if left ≥ right

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

📘 Loose Equality (==) — Type Coercion Happens

console.log(5 == "5"); // true

Explanation:

  • The number 5 and string "5" are not the same type.
  • JavaScript coerces the string to a number, so it becomes 5 == 5, which is true.

⚠️ Warning: Loose equality can lead to unexpected results.

console.log(false == 0); // true
console.log(null == undefined); // true
console.log([] == false); // true

💡 Tip: Avoid == unless you’re 100% sure how coercion works.


📘 Strict Equality (===) — No Type Conversion

console.log(5 === "5"); // false

Explanation:

  • 5 (number) !== "5" (string)
  • No coercion happens — it checks both value and type.

🧪 Inequality Operators: != and !==

These are the inverse of the equality checks:

console.log(5 != "5");   // false (coercion makes it equal)
console.log(5 !== "5");  // true (different types)

📘 Always prefer !== for accurate, predictable comparisons.


📉 Relational Operators: <, >, <=, >=

These operators compare values and follow mathematical logic. If operands are of different types, they’re often converted to numbers.

console.log(10 > 5);       // true
console.log("2" < "10");   // false (lexical string comparison)

Explanation:

  • "2" < "10" compares strings, not numbers. It checks "2" vs "1""2" is greater → result is false.

💡 Tip: Convert strings to numbers before comparing numerically:

console.log(Number("2") < Number("10")); // true

🧊 Special Case: null, undefined, NaN

🔸 null and undefined:

console.log(null == undefined);  // true
console.log(null === undefined); // false

Explanation:

  • Loosely equal, but not the same type.

🔸 NaN:

console.log(NaN == NaN);   // false
console.log(NaN === NaN);  // false

💡 Use isNaN() or Number.isNaN() for checking:

console.log(Number.isNaN(NaN)); // true

🧱 Object Comparison: Reference, Not Value

When comparing objects or arrays, JavaScript compares references, not contents.

const a = { name: "JS" };
const b = { name: "JS" };
console.log(a === b); // false

✅ Even though both objects have the same properties, they point to different memory locations.

💡 Tip: To compare object content, convert to JSON or loop through properties:

JSON.stringify(a) === JSON.stringify(b); // true

✅ Best Practices for Comparisons

  • 🔒 Use === and !== for all equality checks.
  • ⚠️ Avoid relying on JavaScript’s type coercion unless explicitly needed.
  • 📉 Always cast values to numbers before doing numeric comparisons.
  • 📘 Use Object.is() when you need to distinguish +0 and -0, or check NaN.
Object.is(NaN, NaN); // true
Object.is(+0, -0);   // false

📌 Summary — Comparing Values the Right Way

Mastering JavaScript comparisons helps you write more robust, bug-free logic. Here’s what to remember:

  • Always default to === and !== for safety 🧯
  • Avoid tricky coercions with ==, !=
  • Understand object vs primitive comparison 🧠
  • Use helper methods like Number.isNaN() and Object.is() for edge cases

❓FAQs — JavaScript Comparison Operators

❓ What’s the difference between == and === in JavaScript?
🔹 == allows type coercion, while === requires both value and type to match.

❓ Why does NaN == NaN return false?
🔹 Because NaN is defined as not equal to anything, including itself. Use Number.isNaN() to check.

❓ How are objects compared in JavaScript?
🔹 Objects are compared by reference, not value. Two objects with the same structure are not ===.

❓ Should I ever use == in JavaScript?
🔹 It’s safer to use === in most cases. Use == only when you understand how coercion works.

❓ What’s the safest way to compare complex objects?
🔹 Convert them to JSON strings with JSON.stringify() or use deep comparison utilities like lodash.isEqual.


Share Now :

Leave a Reply

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

Share

JavaScript — Comparisons

Or Copy Link

CONTENTS
Scroll to Top