-2

I was just playing around with NaN and isNaN(). Interestingly, I found out that "type of" NaN is a number! And naturally as a curious learner, I thought, "if NaN is a number, like 4, then isNaN(NaN) should also return FALSE, like isNaN(4) is FALSE".

So I conducted a simple experiment on this. And of course, it returns TRUE! I understand that "okay, NaN is NaN so isNaN(NaN) is true". But I am more interested the thinking process of JavaScript. How does JS evaluate isNaN(NaN) to be true, when typeof NaN is NUMBER? Code shown below is what I did to explore this problem:

let test = 4; console.log(1. We will test this value: ${test}. 1-A. Typeof yields: ${typeof test}; 1-B. isNaN() yields: ${isNaN(test)}; 1-C. Number.isNaN() yields: ${Number.isNaN(test)})

test = NaN; console.log(2. We will test this value: ${test}. 2-A. Typeof yields: ${typeof test}; 2-B. isNaN() yields: ${isNaN(test)}; 2-C. Number.isNaN() yields: ${Number.isNaN(test)})

The result was: 1-A/1-B/1-C = number/false/false 2-A/2-B/2-C = number/true/true

As I explained before, this result makes sense to me, that NaN IS in fact NaN. But I am more interested in the thinking process of JS. How did it evaluate the above result, when the typeof was both number?

sanyassh
  • 8,100
  • 13
  • 36
  • 70
jshji
  • 196
  • 1
  • 8
  • 3
    Possible duplicate of [Confusion between isNaN and Number.isNaN in javascript](https://stackoverflow.com/questions/33164725/confusion-between-isnan-and-number-isnan-in-javascript) – rafaelcastrocouto Apr 27 '19 at 12:02
  • 1
    Here's a nice article to explain the NaN !== NaN issue http://adripofjavascript.com/blog/drips/the-problem-with-testing-for-nan-in-javascript.html – rafaelcastrocouto Apr 27 '19 at 12:04
  • 1
    `isNaN` checks whether the value you passed is the `NaN` value. It does not check `typeof arg != "number"`, that's not what NotANumber means. – Bergi Apr 27 '19 at 12:05
  • Thanks for the comments. The answer that i was looking for was found in the specifications for JS (ECMAScript language specifications)! – jshji Apr 28 '19 at 16:00

1 Answers1

0

Some of the answers in the possible duplicate link have possible answers to this question but I would just like to add that the words of skilled and experienced developers may sometimes be right but the right answer to this question should be below:

To understand how something works is to view the manual describing the specifications and workings of that something.

For example, in this instance, the question is how javascript evaluates the NaN object with the isNaN() function, which is ultimately a question of the specification for the said function and object. Therefore, we look at the description of the isNaN() function found in the ecmascript documentations which also has a description of the Number.isNaN() function for comparison. You can also view the description of what the NaN object is to better understand the concepts behind the results of your tests.

I hope this answer helps.

Ian Preglo
  • 421
  • 2
  • 10
  • Ian, this is exactly the sort of answer that I was asking. I wanted to know HOW JS thought when given the specific task. I did not know about the ECMAScript's standard page that you gave me the link to (which I should know haha), and I am so going to savour them now! Thank you very much for your time and knowledge. Much appreciated. – jshji Apr 27 '19 at 16:11
  • So just in case someone else wants to know the answer, if we look at the link provided above, we can find that isNaN() function checks whether the argument is NaN or not. It does not check whether the argument is a number or not. In the world of JS, NaN belongs to the number type. so 3 is also a number, and NaN also a number here. And since the isNaN() function checks whether or not the argument is NaN, so isNaN(3) is false, because it is NOT a NaN. but isNaN(NaN) is true, because it is a NaN. So NaN is a number, and is a NaN. lol. – jshji Apr 27 '19 at 16:54