Javascript Number isNaN() Function Example
Javascript Number isNaN() is an inbuilt function that returns true if the value is of the type Number, and equates to NaN. Otherwise, it returns false. The Number.NaN() method determines whether the passed value is NaN and its type is Number. The Number.isNaN() method decides whether the value is NaN (Not-A-Number).
Javascript Number isNaN()
The isNaN() method returns true if the value is of the type Number, and equates to NaN. Otherwise, it returns false. Number.isNaN() is different from the global isNaN() function. The global isNaN() function converts the tested value to a Number, then tests it.
Number.isNaN() does not convert the values to a Number, and will not return true for any value that is not of the type Number.
See the following syntax.
Number.isNaN(value)
The value parameter is required, and it is the value that needs to be tested.
#Pass infinite value as a parameter
See the following code.
console.log(Number.isNaN(10/0))
See the output.
➜ es git:(master) ✗ node app false ➜ es git:(master) ✗
#Pass number as a parameter
See the following code.
console.log(Number.isNaN(11))
See the output.
➜ es git:(master) ✗ node app false ➜ es git:(master) ✗
#Pass NaN as a parameter
See the following code.
console.log(Number.isNaN(NaN))
See the output.
➜ es git:(master) ✗ node app true ➜ es git:(master) ✗
#Pass Number in a String as a parameter
See the code.
console.log(Number.isNaN('11'))
See the output.
➜ es git:(master) ✗ node app false ➜ es git:(master) ✗
The global method isNaN() method will return true for the string. See the following code.
console.log(isNaN('Eleven'))
See the output.
➜ es git:(master) ✗ node app true ➜ es git:(master) ✗
So, Number.isNaN() method is a more powerful version of the original, global isNaN().
#Polyfill
The following works because NaN is the only value in javascript which is not equal to itself.
Number.isNaN = Number.isNaN || function(value) { return value !== value; }
Finally, Javascript Number isNaN() Function Example is over.