Java Reference
In-Depth Information
The only way to accurately check if a value is NaN is to check that its type is
a number (because NaN is of type "number") and also check that the isNaN
function returns true . These two conditions should be combined using the
logical AND ( && ) that we saw earlier:
isnan = NaN; // set the variable isnan to be NaN
<< NaN
notnan = "hello"; // set the variable notnan to
"hello"
<< "hello"
typeof(isnan) === "number" && isNaN(isnan);
<< true
typeof(notnan) === "number" && isNaN(notnan);
<< false
So, a JavaScript ninja should always use hard equality when testing if two
values are equal. This will avoid the problems caused by JavaScript's type
coercion.
If you want to check whether a number represented by a string is equal to a
number, you should convert it to a number yourself explicitly:
> Number("5") === 5
<< true
This can be useful when you're checking values entered in a form as these
are always strings.
Inequality
We can check if two values are not equal using the inequality operator. There is a soft in-
equality operator, != and a hard inequality operator, !== . These work in a similar way to
the soft and hard equality operators:
Search WWH ::




Custom Search