JavaScript NaN’s full form is “Not-a-Number“. The global NaN property is a value representing Not-A-Number. The NaN property is a global property that represents the value NaN.
Number.NaNNaN is a property of the global object. In other words, it is a variable in the global scope.
Here are some key points about NaN in JavaScript:
1. Result of invalid arithmetic operations: When certain arithmetic operations or functions in JavaScript encounter invalid operands or return values that cannot be represented as meaningful numbers (e.g., dividing zero by zero), the result is NaN.
2. Data type: NaN is a value of the Number data type.
3. NaN is not equal to anything: Surprisingly, NaN is not equal to itself, which means you cannot check for NaN using the normal equality comparison (==) or strict equality comparison (===) operators. For example:
console.log(NaN === NaN); // false
console.log(NaN == NaN); // falseTo check if a value is NaN, you can use the global function isNaN():
console.log(isNaN(NaN)); // true
console.log(isNaN(42)); // false
console.log(isNaN("Hello")); // true, because converting "Hello" to a number results in NaN
4. Testing for NaN: Prior to ECMAScript 6 (ES6), the common way to check for NaN was using the isNaN() function. However, it had some issues because it attempted to convert non-numbers to numbers before testing. In ES6 and later versions, you can use Number.isNaN() to more accurately check for NaN:
console.log(Number.isNaN(NaN)); // true
console.log(Number.isNaN(42)); // false
console.log(Number.isNaN("Hello")); // false, because it doesn't attempt to convert "Hello" to a number
Note: NaN is a specific value representing “Not-a-Number” and is not equal to any other value, including itself. Therefore, it requires special handling when encountered in mathematical operations or when checking for its presence in a variable.
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:

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: NaNTesting 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); // trueComment if you have any doubts 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