JavaScript Number method: isNaN()
[this page | pdf | back links]
The isNaN() method (of the JavaScript
Number
object) returns true if value is
of type Number and is an integer (within range understood as integers by the
browser), otherwise returns false.
It
has the following syntax with the following parameters:
Number.isNaN(x)
Parameter
|
Required / Optional
|
Description
|
x
|
Required
|
Input parameter
|
The Number.isNaN method is subtly
different to the global isNaN
function. The latter coerces a value to a number before testing it, whilst the
former does not. So, Number.isNaN("NaN")
returns false, whilst isNaN("NaN") returns true.
EXAMPLE:
HTML USED IN THIS EXAMPLE:
<!DOCTYPE html>
<html> <!-- Copyright (c) Nematrian Limited 2018 -->
<head>
<style>
table,th,tr,td {border: 1px solid black; border-collapse: collapse;}
</style>
</head>
<body>
<table>
<tr>
<th>Example</th>
<th>Resulting value of <code>x</code></th>
</tr>
<tr>
<td><code id="Example"></code></td>
<td><code id="Result"></code></td>
</tr>
</table>
<script>
document.getElementById("Example").innerHTML =
'var x = Number.isNaN(0/0);';
document.getElementById("Result").innerHTML
= Number.isNaN(0/0);
</script>
</body>
</html>
|
FUNCTION THAT MAY ASSIST IN TESTING WHETHER FEATURE IS SUPPORTED:
function isSupportedJavaScriptMethodNumberIsNaN() {
var z = 12.5; return !!z.isNaN;
} |
NAVIGATION LINKS
Contents | Prev | Next | JavaScript Numbers and Math