From the Google Chrome console:
var x = null;
undefined
x > 0
false
x < 0
false
x > -1
true
x < 1
true
x == 1
false
x === 1
false
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
When you compare null for equality to 0, the result is false. If you force
nullto be interpreted in a numeric context then it is treated like 0 and the result becomes true.You can force it to be numeric by putting
+in front, or by using numeric operators like<,<=,>, and>=. Notice hownull >= 0andnull <= 0are both true.The ECMAScript Language Specification defines when a so-called “ToNumber” conversion is performed. When it is, null and false are both converted to 0.
Knowing when the ToNumber conversion is applied depends on the operator in question. For the relational operators
<,<=,>, and>=see:The
==operator is different. Its type conversions are described below. Notice how null and false follow different rules.If you read carefully you can see why
false == 0is true butnull == 0is false.For
false == 0, Type(x) is Boolean. This means Step 18’s type conversion is applied, and false is converted to a number. ToNumber(false) is 0, and0 == 0is true, so the comparison succeeds.For
null == 0, Type(x) is Null. None of the type checks match so the comparison falls through to Step 22, which returns false. The comparison fails.