Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING INTRO TO HTML & CSS BASH RUST TOOLS

JS Tutorial

JS Home JS Introduction JS Where To JS Output JS Syntax JS Operators JS If Conditions JS Loops JS Strings JS Numbers JS Functions JS Objects JS Scope JS Dates JS Temporal  New JS Arrays JS Sets JS Maps JS Iterations JS Math JS RegExp JS Data Types JS Errors JS Debugging JS Style Guide JS Reference JS Projects  New JS Versions JS HTML DOM JS HTML Events JS HTML First

JS Advanced

JS Functions JS Objects JS Classes JS Asynchronous JS Modules JS Meta & Proxy JS Typed Arrays JS DOM Navigation JS Windows JS Web API JS AJAX JS JSON JS jQuery JS Graphics JS Examples JS Reference


JavaScript NaN

In JavaScript, NaN is short for Not a Number.

NaN is a JavaScript number that is not a legal number.

Invalid Number Operations

You get NaN when JavaScript cannot calculate a number.

Example

let x = 100 / "Apple";

document.getElementById("demo").innerHTML = x;
Try it Yourself »

NaN is a Number

The type of NaN is number.

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

Example

let x = NaN;

document.getElementById("demo").innerHTML = typeof x;
Try it Yourself »

Numeric Strings

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

Example

let x = 100 / "10";

document.getElementById("demo").innerHTML = x;
Try it Yourself »

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


Non-Numeric Strings

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

Example

let x = 100 / "Apple";

document.getElementById("demo").innerHTML = x;
Try it Yourself »

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


Using isNaN()

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

Example

let x = 100 / "Apple";

document.getElementById("demo").innerHTML = isNaN(x);
Try it Yourself »

NaN is Not Equal to Itself

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

Example

let x = NaN;

document.getElementById("demo").innerHTML = x == x;
Try it Yourself »

To test for NaN, use isNaN().


NaN in Math

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

Example

let x = NaN;
let y = 5;

document.getElementById("demo").innerHTML = x + y;
Try it Yourself »

Note

NaN means Not a Number.

But the type of NaN is number.

Use isNaN() to check if a value is NaN.


×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2026 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.

-->