NewProgrammer
NewProgrammer

Reputation: 164

If I have something that evaluates to NaN why doesn't it show equality to NaN

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

Upvotes: 1

Views: 75

Answers (1)

Mudit
Mudit

Reputation: 54

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

Upvotes: 1

Related Questions