NaN

NaN is short for Not a Number. In JavaScript, dividing a number by 0 returns NaN.

Any operation involving NaN always returns NaN.

 
<!DOCTYPE html>
<html>
<head>
    <title>Number Example</title>
    <script type="text/javascript">
          
            document.writeln(NaN /10); 

       
    </script>
</head>
<body>
</body>
</html>
  
Click to view the demo

NaN is not equal to any value, including NaN.

 
<!DOCTYPE html>
<html>
<head>
    <title>Number Example</title>
    <script type="text/javascript">
            document.writeln(NaN == 0); //false 
            document.writeln(NaN == NaN); //false 

       
    </script>
</head>
<body>
</body>
</html>
  
Click to view the demo

JavaScript isNaN() function tells if the value is "not a number".

 
<!DOCTYPE html>
<html>
<head>
    <title>Number Example</title>
    <script type="text/javascript">
        document.writeln(isNaN(NaN)); //true 
        document.writeln(isNaN(10)); //false - 10 is a number 


    </script>
</head>
<body>
</body>
</html>
  
Click to view the demo

isNaN tries to convert the value passed to number.

 
<!DOCTYPE html>
<html>
<head>
    <title>Number Example</title>
    <script type="text/javascript">
        document.writeln(isNaN("10")); //false - can be converted to number 10 
    </script>
</head>
<body>
</body>
</html>
  
Click to view the demo

true is converted to 1 and false is converted to 0.

 
<!DOCTYPE html>
<html>
<head>
    <title>Number Example</title>
    <script type="text/javascript">
        document.writeln(isNaN(true)); //false - can be converted to number 1 
        document.writeln(isNaN(false)); //false - can be converted to number 0 
    </script>
</head>
<body>
</body>
</html>
  
Click to view the demo
Home 
  JavaScript Book 
    Language Basics  

Data Types:
  1. JavaScript Data Types
  2. typeof Operator
  3. The Undefined Type
  4. null Type
  5. null vs undefined
  6. Boolean Type
  7. Boolean() casting function
  8. The Literials of Number Type
  9. Octal Integer
  10. Hexadecimal
  11. Floating-Point Values
  12. Value range
  13. NaN
  14. Number Conversions:Number(), parseInt() and parseFloat()
  15. Number() function
  16. parseInt()
  17. parseFloat()
  18. The String Type
  19. String Literals and Escapes
  20. Get the String Length
  21. Converting to a String with toString() method
  22. Convert Number to String with radix
  23. Convert to String with String() casting function