The global isNaN() function asks whether a value becomes NaN after numeric conversion. For stricter checks, Number.isNaN() tests only whether a value is actually the special numeric value NaN.
console.log(isNaN("25")); // false: "25" converts to 25
console.log(Number.isNaN("25")); // false: the value is a string, not NaN
console.log(Number.isNaN(NaN)); // true
isNaN(value) first converts value to a number. It returns true when the converted result is NaN. Because conversion happens first, the result can be surprising when you are really trying to check a value's type.
console.log(isNaN(2)); // false
console.log(isNaN(2.4)); // false
console.log(isNaN("2.4")); // false
console.log(isNaN("Hello")); // true
console.log(isNaN("5ab")); // true
console.log(isNaN()); // true: undefined becomes NaN
Use if...else when your program needs different behavior for valid and invalid numeric input.
Number.isNaN(value) does not coerce its argument. It returns true only for the numeric value NaN.
console.log(Number.isNaN(NaN)); // true
console.log(Number.isNaN("hello")); // false
console.log(Number.isNaN("25")); // false
console.log(isNaN("")); // false: "" becomes 0
console.log(isNaN(" ")); // false: whitespace becomes 0
console.log(isNaN(true)); // false: true becomes 1
console.log(isNaN(null)); // false: null becomes 0
An empty form field should usually be treated as missing input, even though global isNaN("") returns false. Check for empty or whitespace-only input before numeric conversion.
const raw = " ";
if (raw.trim() === "") {
console.log("Enter a value");
}
For a field that must contain one complete number, trim it, reject an empty value, convert with Number(), and then test the result.
function readNumber(raw) {
const text = raw.trim();
if (text === "") return null;
const value = Number(text);
return Number.isNaN(value) ? null : value;
}
The older interactive example is still available at isNaN demo.
parseInt() and parseFloat() can return NaN when parsing cannot start with a valid number. Test the parsed result with Number.isNaN().
const whole = parseInt("42px", 10); // 42
const bad = parseFloat("px42"); // NaN
console.log(Number.isNaN(bad)); // true
Do not use value === NaN; NaN is not equal to itself. Also do not assume !isNaN(value) means the original value is a JavaScript number—the global function may have converted a string, Boolean, empty string, or null.
console.log(NaN === NaN); // false
console.log(Number.isNaN(NaN)); // true
Continue with NaN in JavaScript, Number.isInteger(), parseInt(), and parseFloat().
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.
| Me2 | 07-03-2009 |
| Cool | |
| tcx | 11-10-2009 |
| yes, agree, this is very useful topic it helps me in project, good concept not finding in other sites | |
| Dhaval Shingala | 25-02-2010 |
| The simplest Way is Why u check entered input is Number or not just check on Keypress Event is number or not The Function for checking Is number is function isNumberKey(B,C){if(!C){C=0}var A=(B.which)?B.which:event.keyCode;if(A>31 | |
| Rakesh Joshi | 16-04-2010 |
| I observed that isNaN has a flaw. If we enter a value with alphabets at the start, it will detect. But if it starts with a number and continues with alphabets or ends with alphabets, then isNaN will return false, meaning to say it is a number, which is not the desired output. Is there any other alternative? | |
01-09-2010 | |
| write a javascript code that will pop-up when an input of not a number is enter and return true when number data is enter. but without using in inbuild function such as isNan. | |
| angelito | 09-06-2012 |
| This is okay.. | |
| Ray | 30-08-2012 |
| Use Integer.parseInt("value") instead and put that in a try catch block. | |
| san | 11-02-2014 |
| @Ray, this is JavaScript not Java. | |
| kamal | 07-08-2014 |
| thanx alot very useful | |