is 0 !== null in Javascript 💡

Sh Raj - Apr 12 - - Dev Community

🧐 JavaScript Quirks: Tricky Equality Questions! 💡

The dev.to markdown prasing has some errors so you can try this on GitHUb Gist :- https://gist.github.com/SH20RAJ/2637af7a368318efa3bace43bfc98f36


Have you ever wondered about the curious world of JavaScript equality comparisons? 🤔 Brace yourself for a dive into some peculiar scenarios where JavaScript's loose equality rules can lead to surprising results! Let's explore these quirky questions together! 🚀✨

Question 1: What's the result of 0 == null? 🤔

console.log(0 == null); // ?
Enter fullscreen mode Exit fullscreen mode

Answer:

The output is:

  false
Enter fullscreen mode Exit fullscreen mode

In JavaScript, null is only equal to undefined, not 0.

Question 2: How about 0 <= null? 🤔

console.log(0 >= null); // ?
Enter fullscreen mode Exit fullscreen mode

Answer:

The output is:

  true
Enter fullscreen mode Exit fullscreen mode

This might seem odd, but in JavaScript, when >= is used with null, it is coerced to 0. So 0 <= null becomes 0 >= 0, which is true.

Question 3: What does 0 <= null return? 🤔

console.log(0 <= null); // ?
Enter fullscreen mode Exit fullscreen mode

Answer:

The output is:

  true
Enter fullscreen mode Exit fullscreen mode

Similar to the previous question, &lt;= with null coerces null to 0, making 0 &lt;= null effectively 0 &lt;= 0, which is true.

Question 4: How does false == null behave? 🤔

console.log(false == null); // ?
Enter fullscreen mode Exit fullscreen mode

Answer:

The output is:

  false
Enter fullscreen mode Exit fullscreen mode

In JavaScript, false is not equal to null. They are of different types and values.

Question 5: What happens with false <= null? 🤔

console.log(false <= null); // ?
Enter fullscreen mode Exit fullscreen mode

Answer:

The output is:

  true
Enter fullscreen mode Exit fullscreen mode

This is another case of coercion. false is coerced to 0 and null to 0, making 0 &lt;= 0, which is true.

Question 6: Lastly, is false >= null true or false? 🤔

console.log(false >= null); // ?
Enter fullscreen mode Exit fullscreen mode

Answer:

The output is:

  true
Enter fullscreen mode Exit fullscreen mode

Similar to the previous cases, both false and null are coerced to 0, so 0 &gt;= 0 is true.

🚀 Ready for the JavaScript Quirks Adventure? 💡

JavaScript's loose equality comparisons can lead to some unexpected results! Keep these quirks in mind as you navigate the JavaScript landscape, and always test your code to understand what's happening under the hood! 🧐✨

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .