JavaScript provides the isNaN() method in order to check whether a given value or variable is a number or not. NaN is the short form the “Not a Number” and checks for negative cases. If the given value or variable is not a number it will return true, if a number it will return false. There is isNaN() and Number. isNaN() function event they provide similar function the interior working methodology and in some cases results are different. We will talk about these below.
Why Use isNan() Function?
Before diving into the isNaN() method we will list why we use it. There may be different cases and causes like below.
- Check given value is a number
- Check if given value is acceptable
- Checking for security in the client side
isNaN() vs Number.isNaN()
As stated previously there are two isNaN() methods. isNaN() method will try to convert the given value or variable into a number and then check if it is number or not but the Number.isNaN() does not try to convert into a number and directly checks the given variable or value if it is a number or not.
isNaN() Method Syntax
isNaN() function has very simple syntax where a single value or variable is provided to this method. isNaN() method provided with all major browsers latest versions like Google Chrome, Mozilla Firefox, Microsoft Edge, Internet Explorer, Opera, Safari and their mobile versions
isNaN(var)
- var is a variable or value which contains some data like 1, 3.4, “Hello” etc. This parameter is a must.
The return type of the isNaN() function is boolean which can return true or false according to the given value or variable.
NaN() Function Examples
Below we will list some examples about the NaN() function with different values and provides the return value like false or true.
isNaN(321)
//false
//Value is a a decimal number
isNaN(-3.45)
//false
//The value is an negative decimal which is a number
isNaN(5-2)
//false
//The result is 3 as a number
isNaN(0)
//false
//The value is 0 which a a number
isNaN('123')
//false
//Even provided value is a string it is converted into number which is valid
isNaN('Hello')
//true
//Provided value is string and can not be converted into number
isNaN('2005/12/12')
//true
//Provided value is string representation of a date an can not be converted into a number
isNaN('')
//false
//There is no value and converted as 0 into number
isNaN(true)
//false
//The boolean value type true is converted as 1 into a number
isNaN(undefined)
//true
//The undenfined do not contains any data which can not be converted into number
isNaN('NaN')
//true
//'NaN' string is not a number because it can not be converted into number
isNaN(NaN)
//true
//NaN string is not a number because it can not be converted into number
isNaN(0 / 0)
//true
//Division zero to zero will return an error which can not be converted into a number
isNaN(null)
//false
//Null is an empty value which is converted into 0
isNaN("Infinity")
//false
//Infinity is converted into a number even it can not a countable number
isNaN(false)
//false
//Boolean false will be converted into 0 which is a number
isNaN(Infinity)
//false
//Infinity will be converted into infinite number which will be false