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 & String | String β Number |
Boolean & Anything | Boolean β Number (true β 1, false β 0) |
Object or Array & Primitive | Object/Array β Primitive via valueOf() or toString() |
null & undefined | No conversion, but treated as equal (special rule) |
null or undefined with others | No 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 | == Result | Why itβs true/false (simple) | === Result | Why itβs true/false |
---|---|---|---|---|
5 == "5" | β true | "5" changes to 5, number 5 stays 5, now β 5 == 5 | β false | Different types: number vs string |
0 == false | β true | false changes to 0, number 0 stays 0, now β 0 == 0 | β false | Different types: number vs boolean |
"1" == true | β true | "1" changes to 1, true also changes to 1, now β 1 == 1 | β false | Different types: string vs boolean |
null == undefined | β true | null and undefined treated equal by JS so it’s treated as true | β false | Different types: null vs undefined |
null == 0 | β false | null stays null, 0 stays 0, no conversion, so it’s treated as false | β false | null and number are not equal |
undefined == 0 | β false | undefined stays undefined, 0 stays 0, no conversion, so it’s treated as false | β false | undefined and number are not equal |
[] == "" | β true | [] changes to "" , "" stays "" now β “” == “” | β false | Array 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 | β false | Array 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 | β false | Array 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 | β false | Array and number are different types |
{ } == "[object Object]" | β true | {} changes to "[object Object]" , "[object Object]" stays "[object Object]" now β “[object Object]” == “[object Object]” | β false | Object and string are different types |
{ } == { } | β false | Two objects have different memory locations so it’s treated as false | β false | Different references |
NaN == NaN | β false | NaN is a special value that doesnβt match anything, not even itself so it’s treated as false | β false | Even 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
Operator | Name | Checks | Type Conversion? |
---|---|---|---|
== | Loose Equality | Value only | β Yes |
=== | Strict Equality | Value 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.