As per my understanding NaN
stands for Not A Number
. Strings are not definitely Numbers and hence I expect the below code to return true
for Strings. However, it's not the case.
console.log(Number.isNaN("Stack Overflow"));
As per my understanding NaN
stands for Not A Number
. Strings are not definitely Numbers and hence I expect the below code to return true
for Strings. However, it's not the case.
console.log(Number.isNaN("Stack Overflow"));
Could somebody please clarify this?
Share Improve this question asked Jul 10, 2017 at 12:01 Jagan NJagan N 2,0651 gold badge14 silver badges31 bronze badges 5-
NaN
is actually a value. – E_net4 Commented Jul 10, 2017 at 12:02 - developer.mozilla/en-US/docs/Web/JavaScript/Reference/… – Teemu Commented Jul 10, 2017 at 12:02
-
5
Number.isNaN
only returns true if its argument isNaN
. It seems weird, but the reason for its existence is falsiness ofNaN === NaN
expression. – raina77ow Commented Jul 10, 2017 at 12:02 - console.log(isNaN("Stack Overflow")); – Shiladitya Commented Jul 10, 2017 at 12:03
- 1 Possible duplicate of Why does typeof NaN return 'number'? – E_net4 Commented Jul 10, 2017 at 12:04
9 Answers
Reset to default 12There is a distinction to be made between Number.isNaN
and isNaN
https://developer.mozilla/en/docs/Web/JavaScript/Reference/Global_Objects/isNaN
The isNaN() function determines whether a value is NaN or not.
https://developer.mozilla/en/docs/Web/JavaScript/Reference/Global_Objects/Number/isNaN
The Number.isNaN() method determines whether the passed value is NaN and its type is Number. It is a more robust version of the original, global isNaN().
The reason you are returning false is that "Stack Overflow" is not a number it is a string.
console.log('Number.isNaN("Stack Overflow"): '+Number.isNaN("Stack Overflow"));
console.log('isNaN("Stack Overflow"): '+isNaN("Stack Overflow"));
https://developer.mozilla/en/docs/Web/JavaScript/Reference/Global_Objects/Number/isNaN#Description
In parison to the global isNaN() function, Number.isNaN() doesn't suffer the problem of forcefully converting the parameter to a number. This means it is now safe to pass values that would normally convert to NaN, but aren't actually the same value as NaN. This also means that only values of the type number, that are also NaN, return true.
This returns true:
console.log(isNaN("Stack Overflow"));
Whereas this returns false:
console.log(Number.isNaN("Stack Overflow"));
It's not Number.isNaN.
console.log(isNaN("Stack Overflow"));
I had to do this
Number.isNaN(Number("Stack Overflow")) // returns true
Number.isNaN(Number(false)) // returns false (unfortunately)
Number.isNaN(Number(12)) // returns false
Number.isNaN(Number(12.02)) // returns false
This seems weird but once understood that NaN means "Not a Number" in a strict sense described here Why does typeof NaN return 'number'? (division by zero, etc), this is what I use:
typeof value === "number" && !isNaN(value)
I'll just add something here which makes it somewhat clear for me.
For my purposes there's a distinction between 'not a number' and Nan.
'not a number' could be any other primitive type that is not a number.
NaN is the numeric value Number.NaN.
console.log(isNaN("StackOverflow")); => true as it ONLY checks if the value is 'not a number'
console.log(isNaN("3")); => false as isNaN does a conversion before checking if the input is 'not a number'
If a = NaN and b = Number.NaN.
All a are b, but not all b are a.
We would only get Number.IsNaN() = true (as all a are b we will also have IsNan() = true for these cases) in specific cases:
- Where a conversion fails: parseInt("a");
- Where we perform a mathematical operation which does not return a real number: Math.sqrt(-1);
- Indeterminite form (0 * Infinity)
- Where we use Nan as an operand: 7 * NaN (NaN is contageous!)
- Where we want to represent an invalid value as a number: new Date("a").getTime();
try that:
console.log(isNaN("Stack Overflow"));
According to MDN, non-numeric values are first coerced to a number, and then tested for NaN. So, empty string (""
) in JS is considered falsy, and so is converted to 0, which is definitely a number.