While working in javascript, we get a requirement to determine if a value is NaN in javascript. This article demonstrates easy ways to find out if a particular variable is NaN.
NaN in javascript is a global property with the value representing Not-a-Number.
1. Check if a Value is NaN using isNaN() method
The method isNaN(_value) is used to check if a value is a NaN or not. Here, _value is the value to test. This method will return true if a value is NaN. Else will return false.
The method Number.isNaN(_value) is used to check if a value is a NaN or not, and its type is Number. This method will return true if a value is NaN. Else will return false. Here, _value is the value to test.
Example:-
Check if the three values ‘Javascript123’, ‘1234’, 1234 are NaN or not.
Code:-
// create a new function checkIfNaN() function checkIfNaN(_variable) { if(isNaN((_variable))) { console.log('Variable is NaN.'); } else { console.log('Variable is NOT a NaN.'); } }; let dummyVariable1 = 'Javascript123'; let dummyVariable2 = '1234'; let dummyVariable3 = 1234; //usage of function checkIfNaN() checkIfNaN(dummyVariable1); checkIfNaN(dummyVariable2); checkIfNaN(dummyVariable3);
Output:-
Variable is NaN. Variable is NOT a NaN. Variable is NOT a NaN.
Explanation:-
Frequently Asked:
- Javascript: String replace all spaces with underscores (4 ways)
- Javascript: Get unique values in an array
- Javascript: Get min element of an array
- Javascript – How to use ?: / Conditional / Ternary Operator
Within this method, the isNaN() method checks if a particular value is NaN or not if the value is NaN, “Variable is NaN.” is printed else “‘Variable is NOT a NaN.'” is printed on the console.
2. Check if a Value is NaN using value !== value
Example:-
Check if the three values ‘Javascript123’, ‘1234’, 1234 are NaN or not.
Code:-
// create a new function checkIfNaN() function checkIfNaN(_variable) { value = Number(_variable); return value !== value; }; let dummyVariable1 = 'Javascript123'; let dummyVariable2 = '1234'; let dummyVariable3 = 1234; //usage of function checkIfNaN() checkIfNaN(dummyVariable1); checkIfNaN(dummyVariable2); checkIfNaN(dummyVariable3);
Output:-
true false false
Explanation:-
Here, in the above code, we are creating a function checkIfNaN() that will check if a value is NaN or not.
Within the method, we check if the value !== value, Only NaN will compare unequal to any other value including another NaN.
3. Check if a Value is NaN using toString()
Javascript’s Number.toString() method returns the string representation of the specified Number object.
Example:-
Check if the values ‘Javascript123’, ‘1234’ are NaN or not.
Code:-
// create a new function checkIfNaN() function checkIfNaN(_variable) { return _variable.toString() === 'NaN' && typeof _variable !== 'string' && typeof _variable === 'number' } let dummyVariable1 = Number('Javascript123'); let dummyVariable2 = Number('1234'); //usage of function checkIfNaN() console.log(checkIfNaN(dummyVariable1)) console.log(checkIfNaN(dummyVariable2))
Output:-
true false
Explanation:-
Here, in the above code, we are creating a function checkIfNaN() that will check if a value is NaN or not.
We check if the toString() on the Number value is ‘NaN’ and typeOf a variable is not a string but a number within the method. If all three conditions are fulfilled, true is returned, which means the value is NaN else false is returned.
4. Check if a Value is NaN using includes()
Javascript’s includes() method determines if a particular value is included in an array or not.
Example:-
Check if the three values ‘Javascript123’, ‘1234’ are NaN or not.
Code:-
// create a new function checkIfNaN() function checkIfNAN(_variable) { return [Number(_variable)].includes(NaN); } let dummyVariable1 = Number('Javascript123'); let dummyVariable2 = Number('1234'); //usage of function checkIfNaN() console.log(checkIfNaN(dummyVariable1)) console.log(checkIfNaN(dummyVariable2))
Output:-
true false
Explanation:-
The above code shows another way to check if the value NaN is included in a number array. This array contains only one value: the variable passed as the argument into method checkIfNaN(). If the value is NaN true is returned else false is returned.
I hope this article helped you check if a value is NaN or not. Good Luck!!!