NaN - Not a Number

  1. NaN is a JavaScript reserved word indicating that a value is not a number. Trying to do arithmetic with a non-numeric string will result in NaN (Not a Number)
  2. However, if the string contains a numeric value, the result will be a number
  3. You can use the global JavaScript function isNaN() to find out if a value is a number
  4. Watch out for NaN. If you use NaN in a mathematical operation, the result will also be NaN
  5. Or the result might be a concatenation
  6. NaN is a number, and typeof NaN returns Number

HTML file: Displayed by browser:
<body>
<h4>#1: A number divided by a non-numeric string becomes NaN (Not a Number):</h4>
<p id="demo1"></p>
<hr />
<h4>#2: A number divided by a numeric string becomes a number:</h4>
<p id="demo2"></p>
<hr />
<h4>#3: Find out if a value is Not a Number with the isNaN() function::</h4>
<p id="demo3"></p>
<hr />
<h4>#4: If you use NaN in a mathematical operation, the result will also be NaN:</h4>
<p id="demo4"></p>
<hr />
<h4>#5: If you use NaN in a mathematical operation, the result can be a concatination:</h4>
<p id="demo5"></p>
<hr />
<h4>#6: The typeof NaN is:</h4>
<p id="demo6"></p>
<script>
// THIS IS #1
    document.getElementById("demo1").innerHTML =
    "100 / \"Apple\" = " + 100 / "Apple" + " (Not a Number)";
// THIS IS #2
    document.getElementById("demo2").innerHTML =
    "100 / \"10\" = " + 100 / "10";
// THIS IS #3
    document.getElementById("demo3").innerHTML =
    "isNaN(100 / \"Apple\") = " + isNaN(100 / "Apple") + " (see #1)";
// THIS IS #4
    var x = NaN;
    var y = 5;
    var z = x + y;
    document.getElementById("demo4").innerHTML =
    " if x = NaN and y = 5 and x + y = z, then z = " + z +
<br />    ", therefore x + y = " + z;
// THIS IS #5
    document.getElementById("demo5").innerHTML =
    " x + y = " + x + y;
// THIS IS #6
    document.getElementById("demo6").innerHTML =
    typeof NaN;
</script>
</body>

#1: A number divided by a non-numeric string becomes NaN (Not a Number):


#2: A number divided by a numeric string becomes a number:


#3: Find out if a value is Not a Number with the isNaN() function::


#4: If you use NaN in a mathematical operation, the result will also be NaN:


#5: If you use NaN in a mathematical operation, the result can be a concatination:


#6: The typeof NaN is:


BackTable of ContentsNext