From 7a9fa7d989cd59732d57c6dd74112066451a1b19 Mon Sep 17 00:00:00 2001 From: chandumandapalli Date: Thu, 6 Mar 2025 22:50:02 +0530 Subject: [PATCH 1/2] Update en-US.mdx --- .../en-US.mdx | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/questions/what-is-the-difference-between-double-equal-and-triple-equal/en-US.mdx b/questions/what-is-the-difference-between-double-equal-and-triple-equal/en-US.mdx index f824108..13306ef 100644 --- a/questions/what-is-the-difference-between-double-equal-and-triple-equal/en-US.mdx +++ b/questions/what-is-the-difference-between-double-equal-and-triple-equal/en-US.mdx @@ -39,6 +39,64 @@ However, when using `==`, unintuitive results can happen: As a general rule of thumb, never use the `==` operator, except for convenience when comparing against `null` or `undefined`, where `a == null` will return `true` if `a` is `null` or `undefined`. + +Let's break down each of the comparisons: + +1️⃣ null == undefined → true +Why? +null and undefined are loosely equal (==) but not strictly equal (===). +JavaScript defines a special rule: +null == undefined; // true + +However: +null === undefined; // false +(because === checks both type and value). + +console.log(null == undefined); // true +console.log(null === undefined); // false + +📌 Key Rule: +null and undefined are only equal to each other but not to anything else. + +2️⃣ [] == false → true +Why? +[] (empty array) is truthy, but when compared with false, it gets coerced into a primitive value. +JavaScript converts false to a number (0). +Then, it converts [] to a string ('') → and an empty string is also 0 when converted to a number. +So, the comparison becomes: + +Number([]) == Number(false); +0 == 0; // true + +console.log([] == false); // true +console.log([] == 0); // true +console.log(Number([])); // 0 +📌 Key Rule: +An empty array [] converts to 0 in numeric comparison. + +3️⃣ '' == false → true +Why? +JavaScript converts false to 0. +An empty string '' also converts to 0 in numeric comparisons. + +So the comparison becomes: +Number('') == Number(false); +0 == 0; // true +Example +console.log('' == false); // true +console.log('' == 0); // true +console.log(Number('')); // 0 +📌 Key Rule: +An empty string '' converts to 0 in numeric comparison. + +🔹 Summary of Type Coercion Rules +null == undefined Special case in JS ✅ true +[] == false ------ [] → '' → 0, false → 0, so 0 == 0 ✅ true +'' == false ------ '' → 0, false → 0, so 0 == 0 ✅ true +📌 ⚠️ Important Note: +These type coercion behaviors can be confusing, which is why it's recommended to use === (strict equality) to avoid unexpected results. + + ```js var a = null; console.log(a == null); // true From dd72e1bc84c3ae422840cc304fdb83ffe3ad5ee9 Mon Sep 17 00:00:00 2001 From: chandumandapalli Date: Sun, 9 Mar 2025 11:00:17 +0530 Subject: [PATCH 2/2] Update en-US.mdx --- .../en-US.mdx | 62 +++++++++---------- 1 file changed, 28 insertions(+), 34 deletions(-) diff --git a/questions/what-is-the-difference-between-double-equal-and-triple-equal/en-US.mdx b/questions/what-is-the-difference-between-double-equal-and-triple-equal/en-US.mdx index 13306ef..ba1a41c 100644 --- a/questions/what-is-the-difference-between-double-equal-and-triple-equal/en-US.mdx +++ b/questions/what-is-the-difference-between-double-equal-and-triple-equal/en-US.mdx @@ -42,59 +42,53 @@ As a general rule of thumb, never use the `==` operator, except for convenience Let's break down each of the comparisons: -1️⃣ null == undefined → true +1. `null == undefined` → true Why? -null and undefined are loosely equal (==) but not strictly equal (===). +`null` and `undefined` are loosely equal (`==`) but not strictly equal (`===`). JavaScript defines a special rule: -null == undefined; // true +`null == undefined; // true` However: -null === undefined; // false -(because === checks both type and value). +`null === undefined; // false` +(because `===` checks both type and value). +```js console.log(null == undefined); // true console.log(null === undefined); // false +``` -📌 Key Rule: -null and undefined are only equal to each other but not to anything else. - -2️⃣ [] == false → true -Why? -[] (empty array) is truthy, but when compared with false, it gets coerced into a primitive value. -JavaScript converts false to a number (0). -Then, it converts [] to a string ('') → and an empty string is also 0 when converted to a number. -So, the comparison becomes: +Key Rule: null and undefined are only equal to each other but not to anything else. -Number([]) == Number(false); +[] == false → true Why? [] (empty array) is truthy, but when compared with false, it gets coerced into a primitive value. JavaScript converts false to a number (0). Then, it converts [] to a string ('') → and an empty string is also 0 when converted to a number. So, the comparison becomes: +Number([]) == Number(false); 0 == 0; // true +```js console.log([] == false); // true console.log([] == 0); // true console.log(Number([])); // 0 -📌 Key Rule: -An empty array [] converts to 0 in numeric comparison. +``` -3️⃣ '' == false → true -Why? -JavaScript converts false to 0. -An empty string '' also converts to 0 in numeric comparisons. +Key Rule: An empty array [] converts to 0 in numeric comparison. -So the comparison becomes: -Number('') == Number(false); -0 == 0; // true -Example +'' == false → true Why? JavaScript converts false to 0. An empty string '' also converts to 0 in numeric comparisons. +So the comparison becomes: Number('') == Number(false); 0 == 0; // true + +```js console.log('' == false); // true console.log('' == 0); // true console.log(Number('')); // 0 -📌 Key Rule: -An empty string '' converts to 0 in numeric comparison. - -🔹 Summary of Type Coercion Rules -null == undefined Special case in JS ✅ true -[] == false ------ [] → '' → 0, false → 0, so 0 == 0 ✅ true -'' == false ------ '' → 0, false → 0, so 0 == 0 ✅ true -📌 ⚠️ Important Note: -These type coercion behaviors can be confusing, which is why it's recommended to use === (strict equality) to avoid unexpected results. +``` + +Key Rule: An empty string '' converts to 0 in numeric comparison. + +Summary of Type Coercion Rules: + +null == undefined - Special case in JS - true +[] == false - [] → '' → 0, false → 0, so 0 == 0 - true +'' == false - '' → 0, false → 0, so 0 == 0 - true +Important Note: These type coercion behaviors can be confusing, which is why it's recommended to use === (strict equality) to avoid unexpected results. + ```js