1

In the following code:

var notNum = 'dfsd'
Number(notNum) === NaN 

The last expression evaluates to false. Do you know why? Is there no way to use NaN in a comparison?

typeof(Number(notNum)) === 'number'

This somehow evaluates to true. I really don't understand how NaN works..

Marco Altieri
  • 3,726
  • 2
  • 33
  • 47
NewProgrammer
  • 164
  • 3
  • 16
  • 4
    Possible duplicate of [Why is NaN not equal to NaN?](https://stackoverflow.com/questions/10034149/why-is-nan-not-equal-to-nan) – JJJ May 26 '18 at 17:30
  • From the [MDN page on NaN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/NaN#Testing_against_NaN): "`NaN` compares unequal (via `==`, `!=`, `===`, and `!==`) to any other value -- including to another `NaN` value." – Scott Sauyet May 26 '18 at 17:34
  • If you're looking to check whether a value is `NaN` --> https://stackoverflow.com/questions/30314447/how-do-you-test-for-nan-in-javascript – JJJ May 26 '18 at 17:40

1 Answers1

1

NaN (Not-a-Number) is a global object in JS returned when some mathematical operation gets failed.

You can't compare an object with another object directly. Either you have to use typeof which you are using or you can use Object.is()

Object.is(Number(notNum), NaN) // true

Mudit
  • 54
  • 6