1

As typeof(NaN) is a number it's value should be numeric but as per the below code it's not numeric. I want to understand the background process behind below code snippet.

var checkVal = NaN;

if (checkVal > 0) {
  console.log("NaN is greater than 0");
} else {
  console.log("NaN is less than 0");
}

if (checkVal < 0) {
  console.log("NaN is less than 0");
} else {
  console.log("NaN is greater than 0");
}

if (checkVal == 0) {
  console.log("NaN is equal to 0");
} else {
  console.log("NaN is not equal to 0");
}

In above code snippet, In every condition it goes into the else block which makes us confused to know the value of NaN.

As per the Bergi comment, value of NaN is NaN.Hence, checkVal === NaN should be true but it is returning false.

var checkVal = NaN;

if (checkVal === NaN) {
  console.log("true");
} else {
  console.log("false");
}
Debug Diva
  • 26,058
  • 13
  • 70
  • 123
  • 6
    `NaN` behaves as is [documented](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/NaN). Just accept it. It is intentional behaviour. There is no "why" – trincot Jun 09 '18 at 18:48
  • 1
    Please ask a single question per post. – Bergi Jun 09 '18 at 18:49
  • The value of `NaN` is the `NaN` value. – Bergi Jun 09 '18 at 18:50
  • 1
    @Bergi if that is true, then why is `NaN == NaN` false? apparently, it's not NaN after all. – hanshenrik Jun 09 '18 at 18:51
  • 1
    @Bergi all the questions are related to `NaN` . Hence, is it required to ask three questions seperately ? Can you please reopen it as it is necessary to understand the logic behind them as it is also a part of interview questions. – Debug Diva Jun 09 '18 at 18:54
  • 2
    More importantly: it is required to first look whether your question has been asked before. Very similar questions have been asked before. Look at the links that have been added in the closure reason. – trincot Jun 09 '18 at 18:56
  • @trincot I checked and did not got the answers in other posts. I looked into lot of SO posts but did not get clarification. In other posts they are giving the solution of how to check for NaN and all but here my question is different. I want to understand the background logic behind those scenarios. – Debug Diva Jun 09 '18 at 19:00
  • 1
    I just have to disagree with you: There is a lot of background there. It is not just JavaScript related, but IEEE754 related. There is one question I referred to where the accepted answer explains that background. – trincot Jun 09 '18 at 19:00
  • 2
    @hanshenrik Being the `NaN` value does not mean that comparing it to `NaN` works different from comparing it to other numbers :-) `==` is only a partial relation on floating point values. – Bergi Jun 09 '18 at 19:54
  • @RohitJindal If you can separate them as clearly into different questions as you did in this post, yes they should be separate. And maybe entering them alone would have led you to the already asked questions (that especially #2 and #3 are exact duplicates of). – Bergi Jun 09 '18 at 19:56
  • @Bergi I removed the #2 & #3 from the post. can you please remove that duplicate question tag from this question. – Debug Diva Jun 09 '18 at 20:36
  • 1
    @RohitJindal, the duplicate flag will not be removed. It is clearly duplicate. The uncommon behaviour of comparing NaN with other values (such as you do with 0) is amply explained in earlier questions on StackOverflow. – trincot Jun 09 '18 at 20:55
  • @trincot which earlier question you are refer to ? I was not able to find the answer of my question in the earlier questions. If value of NaN is not a number than what is the value ? – Debug Diva Jun 09 '18 at 20:59
  • The type of `NaN` is number, its value is `NaN`: two things you already know, but maybe you do not realise that type and value are two different concepts. – trincot Jun 09 '18 at 21:06
  • @RohitJindal See all the questions in the "already has an answer" box at the top of the page. I'm not sure I get your question. The `NaN` value is a bit pattern in the domain of floating point numbers, it has a very special meaning that impacts how it will compare. https://en.wikipedia.org/wiki/NaN – Bergi Jun 09 '18 at 21:21

1 Answers1

1

NaN means "Not A Number". It is a special value that denotes the result of a computation that either is impossible (division by zero, f.e.) or cannot be stored using the floating point format. It represents a value that is unknown or cannot be computed (or stored) using the floating point format.

The comparison of NaN with anything else does not make any sense. NaN is not equal even to itself. This happens because it is not a certain value. Being an unknown value, most probably it is not equal to a different unknown value.

Read more in the documenation of NaN.

axiac
  • 68,258
  • 9
  • 99
  • 134