/

HTML / CSS / JavaScript Tutorial

JavaScript Global method: isNaN()

[this page | pdf | back links]

The JavaScript Global isNaN() method returns true if the value equates to NaN (after initial conversion to a Number) otherwise returns false.

 

It has the following syntax with the following parameters:

 

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>
<tr>
<td><code id="Example2"></code></td>
<td><code id="Result2"></code></td>
</tr>
</table>

<script>
document.getElementById("Example").innerHTML =
  'var x = isNaN(0/0);';
document.getElementById("Result").innerHTML
  = isNaN(0/0);
document.getElementById("Example2").innerHTML =
  'var x = isNaN(0/1);';
document.getElementById("Result2").innerHTML
  = isNaN(0/1);
</script>

</body>
</html>


NAVIGATION LINKS
Contents | Prev | Next | JavaScript DOM (and BOM)


Desktop view | Switch to Mobile