Skip to content

JavaScript NaN property | Not-A-Number

  • by

JavaScript NaN’s full form is “Not-a-Number”. The global NaN property is a value representing Not-A-Number.

Number.NaN

NaN is a property of the global object. In other words, it is a variable in the global scope.

JavaScript NaN

Simple example code

<!DOCTYPE html>
<html>
<body>

  <script>
    let x = Number.NaN;
    console.log(x)

    let z = NaN;
    console.log(z)

  </script>

</body>
</html> 

Output:

JavaScript NaN property

Standard built-in objects – NaN

function sanitise(x) {
  if (isNaN(x)) {
    return NaN;
  }
  return x;
}

console.log(sanitise('1'));
// Output: "1"

console.log(sanitise('NotANumber'));
// Output: NaN

Testing against NaN

NaN === NaN;        // false
Number.NaN === NaN; // false
isNaN(NaN);         // true
isNaN(Number.NaN);  // true
Number.isNaN(NaN);  // true

function valueIsNaN(v) { return v !== v; }
valueIsNaN(1);          // false
valueIsNaN(NaN);        // true
valueIsNaN(Number.NaN); // true

Do comment if you have any doubt or suggestions on this JS NaN topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *