Struggling with == and === in JavaScript? Learn It Now with Simple Use Cases

If you’ve ever wondered about the difference between == vs === in JavaScript, you’re not alone. These two equality operators look similar but work very differently. It is really important to understand how == (double equals) and === (triple equals) work, as this knowledge can help you write cleaner, bug-free code.

πŸ” What is == (Loose Equality)?

  • Compares only values, not data types.
  • If types differ, JavaScript tries to convert one or both values (called type coercion) before comparing.

Think of it like comparing euros and dollars: you need to convert one currency into the other before you can truly compare.

πŸ”’ What is === (Strict Equality)?

  • It compares both value and data type. No type conversion takes place.
  • If data types differ, it immediately returns false.
  • If data types are same, it compares values and give true or false accordingly.

🧠 Conversion Rules for == (When Types Differ)

JavaScript loves to “help out” when types don’t match using ==. It auto-converts stuff behind the scenes. Here’s its cheat sheet:

When comparing…JavaScript converts…
Number & StringString β†’ Number
Boolean & AnythingBoolean β†’ Number (true β†’ 1, false β†’ 0)
Object or Array & PrimitiveObject/Array β†’ Primitive via valueOf() or toString()
null & undefinedNo conversion, but treated as equal (special rule)
null or undefined with othersNo conversion & result is false

If you’re curious about the in-depth rules behind == and === in JavaScript, check out this detailed explanation on MDN.

πŸ” Full Comparison Table with Examples

Expression== ResultWhy it’s true/false (simple)=== ResultWhy it’s true/false
5 == "5"βœ… true"5" changes to 5, number 5 stays 5, now β†’ 5 == 5❌ falseDifferent types: number vs string
0 == falseβœ… truefalse changes to 0, number 0 stays 0, now β†’ 0 == 0❌ falseDifferent types: number vs boolean
"1" == trueβœ… true"1" changes to 1, true also changes to 1, now β†’ 1 == 1❌ falseDifferent types: string vs boolean
null == undefinedβœ… truenull and undefined treated equal by JS so it’s treated as true❌ falseDifferent types: null vs undefined
null == 0❌ falsenull stays null, 0 stays 0, no conversion, so it’s treated as false❌ falsenull and number are not equal
undefined == 0❌ falseundefined stays undefined, 0 stays 0, no conversion, so it’s treated as false❌ falseundefined and number are not equal
[] == ""βœ… true[] changes to "", "" stays "" now β†’ “” == “”❌ falseArray and string are different types
[] == 0βœ… true[] changes to "" (an empty string), and then "" further converts to 0. The right side 0 remains 0 now β†’ 0 == 0❌ falseArray and number are different types
["0"] == 0βœ… true["0"] changes to "0", and then "0" further converts to 0. The right side 0 remains 0 now β†’ 0 == 0❌ falseArray and number are different types
[1] == 1βœ… true[1] changes to "1" and this "1" further changes to 1, right side 1 stays 1(as it is) now β†’ 1 == 1❌ falseArray and number are different types
{ } == "[object Object]"βœ… true{} changes to "[object Object]", "[object Object]" stays "[object Object]" now β†’ “[object Object]” == “[object Object]”❌ falseObject and string are different types
{ } == { }❌ falseTwo objects have different memory locations so it’s treated as false❌ falseDifferent references
NaN == NaN❌ falseNaN is a special value that doesn’t match anything, not even itself so it’s treated as false❌ falseEven strict check doesn’t match NaN to NaN because they’re always considered not equal

πŸ§ͺ Try These in Your Console

console.log(5 == "5"); // true
console.log(0 == false); // true
console.log("1" == true); // true
console.log([1] == 1); // true
console.log([] == ""); // true
console.log([] == 0); // true
console.log({} == "[object Object]"); // true
console.log(null == undefined); // true
console.log(null == 0); // false
console.log(NaN == NaN); // false

βœ… Best Practice

  • Prefer === and !== to avoid unexpected coercion.
  • Use == only if you fully understand these rules and really want loose comparison.

🎯 Quick Summary

OperatorNameChecksType Conversion?
==Loose EqualityValue onlyβœ… Yes
===Strict EqualityValue and Type❌ No

πŸ“ Final Thought

If you’re unsure whether coercion will cause trouble, always default to using ===. Your code will be cleaner, more predictable, and less prone to bugs.

Leave a Comment

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

Scroll to Top