JavaScript NaN

Back to JS page

Table of Contents

Invalid Number Operations

In JavaScript, NaN is short for Not a Number. NaN is a JavaScript number that is not a legal number.

You get NaN when JavaScript cannot calculate a number.

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript NaN</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>

<h4>Invalid Number Operations</h4>
<p id="demo"></p>

<script>
let x = 100 / "Apple";

document.getElementById("demo").innerHTML = x;
</script>

</body>
</html>

Example 1

Result View Example

NaN is a Number

The type of NaN is number. This may look strange, but NaN belongs to the JavaScript number type.

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript NaN</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>

<h4>NaN is a Number</h4>
<p id="demo"></p>

<script>
let x = NaN;

document.getElementById("demo").innerHTML = typeof x;
</script>

</body>
</html>

Example 2

Result View Example

Numeric Strings

JavaScript tries to convert numeric strings to numbers in arithmetic operations.

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript NaN</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>

<h4>Numeric Strings</h4>
<p id="demo"></p>

<script>
let x = 100 / "10";

document.getElementById("demo").innerHTML = x;
</script>

</body>
</html>

The result is 10, because "10" is converted to the number 10.

Example 3

Result View Example

Non-Numeric Strings

A non-numeric string cannot be converted to a number.

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript NaN</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>

<h4>Non-Numeric Strings</h4>
<p id="demo"></p>

<script>
let x = 100 / "Apple";

document.getElementById("demo").innerHTML = x;
</script>

</body>
</html>

The result is NaN, because "Apple" cannot be converted to a number.

Example 4

Result View Example

Using isNaN()

You can use the JavaScript function isNaN() to find out if a value is not a number.

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript NaN</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>

<h4>Using isNaN()</h4>
<p id="demo"></p>

<script>
let x = 100 / "Apple";

document.getElementById("demo").innerHTML = isNaN(x);
</script>

</body>
</html>

Example 5

Result View Example

NaN is Not Equal to Itself

NaN is the only JavaScript value that is not equal to itself.

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript NaN</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>

<h4>NaN is Not Equal to Itself</h4>
<p id="demo"></p>

<script>
let x = NaN;

document.getElementById("demo").innerHTML = x == x;
</script>

</body>
</html>

To test for NaN, use isNaN().

Example 6

Result View Example

NaN in Math

If you use NaN in a mathematical operation, the result will also be NaN.

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript NaN</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>

<h4>NaN in Math</h4>
<p id="demo"></p>

<script>
let x = NaN;
let y = 5;

document.getElementById("demo").innerHTML = x + y;
</script>

</body>
</html>

Example 7

Result View Example

Note

NaN means Not a Number. But the type of NaN is number. Use isNaN() to check if a value is NaN.

Document

Document in project

You can Download PDF file.

Reference