NaN as in most programming languages means Not A Number. It belongs to the numeric data types(int, long, short, etc). It represents a numeric value that can not be interpreted as a finite value. That is one can not define its value. The use of NaN is quite rare. NaN is a property of the global object and number object. Its basically use to check the user’s numeric input and thus make the program error-free.
- Code 1:
value1 = Math.sqrt( -0.5 );
"First Value : "
+ value1 );
value2 = Math.sqrt( -49 );
"<br />Second Value : "
+ value2 );
"<br />Nan!==Nan: "
"<br />Nan!=Nan: "
First Value : NaN Second Value : NaN Nan!==Nan: true Nan!=Nan: true
- Code 2: Now in the first example we clearly know that we can not calculate the square root of negative numbers and so it leads to NaN. We can clearly see the output.
+ isNaN(x) +
true true true true
Now there are two ways of checking the desired input is NaN. The first method to check is isNaN() and the other is Number.isNaN(). The second method is for numeric data types only. the first method is demonstrated in Code 2. The String variable is non-numeric and hence you will get the output as true(i.e. its a NaN).
Difference between NaN!=Nan and NaN!==NaN:
- The main difference is not on the NaN it’s on the operator single = compare(NaN!=NaN) only the value which is not equal for each NaN
- In the NaN!==Nan compare the type of the value also so the defference is clear now.
- HTML & CSS | Tabindex attribute & Navigation bars
- Google Amp amp-ad
- 'AND' vs '&&' as operator in PHP
- && operator in Java with Examples
- Google AMP amp-accordion
- How to create Nested Accordion using Google AMP amp-accordion?
- How to create Hidden Header using Google AMP amp-accordion?
- How to create Dynamic Header using Google AMP amp-accordion?
- Google AMP amp-facebook-like
- Google AMP amp-carousel
- Google AMP amp-date-countdown
- Google AMP amp-brightcove
- Google AMP amp-bind-recaptcha
- Google AMP amp-autocomplete
- How to disable download option in amp-audio in Google AMP ?
- How to set custom media notification with amp-audio in Google AMP ?
- How to set an image placeholder for amp-anim in Google AMP ?
- How to set audio loop in amp-audio of Google AMP ?
- Google AMP amp-img
- Google AMP amp-image-lightbox
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.