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?