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>
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>
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>
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>
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>
Home
JavaScript Book
Language Basics
JavaScript Book
Language Basics
Data Types:
- JavaScript Data Types
- typeof Operator
- The Undefined Type
- null Type
- null vs undefined
- Boolean Type
- Boolean() casting function
- The Literials of Number Type
- Octal Integer
- Hexadecimal
- Floating-Point Values
- Value range
- NaN
- Number Conversions:Number(), parseInt() and parseFloat()
- Number() function
- parseInt()
- parseFloat()
- The String Type
- String Literals and Escapes
- Get the String Length
- Converting to a String with toString() method
- Convert Number to String with radix
- Convert to String with String() casting function