Java Reference
In-Depth Information
The isNaN() Function
The
isNaN()
function has the following signature:
isNaN(value)
It returns
true
if
value
results in
NaN
when converted to a Number; that is, it returns
true
if
Number(value)
return
NaN
. If the value represents a number,
isNaN()
returns
false
. Notice that
isNaN()
does not only check if
value
is
NaN
; it checks if
value
is a
number or not.
NaN
is not considered a number, so
isNaN(NaN)
returns
true
. If you are
interested in checking where
value
is
NaN
, use the expression
value !== value
.
NaN
is
the only value that is not equal to itself. So,
value !== value
will return
true
if and only if
value
is
NaN
. The following code shows how to use the
isNaN()
function:
printf("isNaN(%s) = %b", NaN, isNaN(NaN));
printf("isNaN('%s') = %b", "123", isNaN('123'));
printf("isNaN('%s') = %b", "Hello", isNaN('Hello'));
printf("isNaN('%s') = %b", "97Hello", isNaN('97Hello'));
printf("isNaN('%s') = %b", "Infinity", isNaN('Infinity'));
printf("isNaN(%s) = %b", "1.89e23", isNaN(1.89e23));
var value = NaN;
if (value !== value) {
print("value is NaN")
}
else {
print("value is not NaN")
}
isNaN(NaN) = true
isNaN('123') = false
isNaN('Hello') = true
isNaN('97Hello') = true
isNaN('Infinity') = false
isNaN(1.89e23) = false
value is NaN
Search WWH ::
Custom Search