π§ 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); // ?
Answer:
The output is:
false
In JavaScript, null
is only equal to undefined
, not 0
.
Question 2: How about 0 <= null
? π€
console.log(0 >= null); // ?
Answer:
The output is:
true
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); // ?
Answer:
The output is:
true
Similar to the previous question, <=
with null
coerces null
to 0
, making 0 <= null
effectively 0 <= 0
, which is true
.
Question 4: How does false == null
behave? π€
console.log(false == null); // ?
Answer:
The output is:
false
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); // ?
Answer:
The output is:
true
This is another case of coercion. false
is coerced to 0
and null
to 0
, making 0 <= 0
, which is true
.
Question 6: Lastly, is false >= null
true or false? π€
console.log(false >= null); // ?
Answer:
The output is:
true
Similar to the previous cases, both false
and null
are coerced to 0
, so 0 >= 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)