5

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?

JSN
  • 2,035
  • 13
  • 27

9 Answers9

10

There is a distinction to be made between Number.isNaN and isNaN

https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/isNaN

The isNaN() function determines whether a value is NaN or not.

https://developer.mozilla.org/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"));
Andrew Bone
  • 7,092
  • 2
  • 18
  • 33
1

https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Number/isNaN#Description

In comparison 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.

Ivan Sivak
  • 7,178
  • 3
  • 36
  • 42
1

This returns true:

console.log(isNaN("Stack Overflow"));

Whereas this returns false:

console.log(Number.isNaN("Stack Overflow"));

AGE
  • 3,752
  • 3
  • 38
  • 60
Lekens
  • 1,823
  • 17
  • 31
0

It's not Number.isNaN.

console.log(isNaN("Stack Overflow"));
prasanth
  • 22,145
  • 4
  • 29
  • 53
Shiladitya
  • 12,003
  • 15
  • 25
  • 38
  • But there IS [Number.isNaN](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Number/isNaN) – Mohit Bhardwaj Jul 10 '17 at 12:10
  • @MohitBhardwaj yes but it does something different – Andrew Bone Jul 10 '17 at 12:10
  • 3
    @AndrewBone yes but the answer should instead explain the difference. Or that though it's there, this case should use `isNaN()` – Mohit Bhardwaj Jul 10 '17 at 12:12
  • @MohitBhardwaj if you want to know more about Number.isNaN, please follow the documentation https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Number/isNaN – Shiladitya Jul 10 '17 at 12:15
  • @Shiladitya Is this the same link that I posted in my first comment? What I meant was your answer could be more useful if you had added the difference between the two. Currently, it's just plainly wrong when it says `It's not Number.isNan`. – Mohit Bhardwaj Jul 10 '17 at 12:19
0

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
Christopher Thomas
  • 4,424
  • 4
  • 34
  • 49
0

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)
dgolive
  • 425
  • 3
  • 8
0

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:

  1. Where a conversion fails: parseInt("a");
  2. Where we perform a mathematical operation which does not return a real number: Math.sqrt(-1);
  3. Indeterminite form (0 * Infinity)
  4. Where we use Nan as an operand: 7 * NaN (NaN is contageous!)
  5. Where we want to represent an invalid value as a number: new Date("a").getTime();
Artexias
  • 195
  • 2
  • 14
-1

try that:

console.log(isNaN("Stack Overflow"));
-2

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.

Oleksii Filonenko
  • 1,551
  • 1
  • 17
  • 27