๐Ÿง  Booleans & Comparisons
Estimated reading: 4 minutes 9 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