DEV Community

Sh Raj
Sh Raj

Posted on

is 0 !== null in Javascript πŸ’‘

🧐 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! 🧐✨

Top comments (0)