{"componentChunkName":"component---src-pages-markdown-remark-fields-slug-js","path":"/engineering/checking-for-nan-in-javascript/","result":{"data":{"markdownRemark":{"id":"0f63d421-ce01-5020-8b12-991083fd5274","excerpt":"You'll learn about JavasScript NaN in this tutorial, how to check whether a value is NaN, and how to effectively handle NaN. What is NaN in Javascript? NaN, in…","html":"<p>You'll learn about JavasScript NaN in this tutorial, how to check whether a value is NaN, and how to effectively handle NaN.</p>\n<h2 id=\"what-is-nan-in-javascript\" style=\"position:relative;\"><a href=\"#what-is-nan-in-javascript\" aria-label=\"what is nan in javascript permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>What is NaN in Javascript?</h2>\n<p>NaN, in JavaScript, can be many things. In fact, it can be almost anything, so long as it is <em>Not a Number</em>. Its type is technically “number” (when evaluated with “typeof”), although it stands for <strong>Not a Number</strong>.  </p>\n<h2 id=\"values-in-nan-javascript\" style=\"position:relative;\"><a href=\"#values-in-nan-javascript\" aria-label=\"values in nan javascript permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Values in NaN Javascript</h2>\n<p>Values can become NaN through a variety of means, which usually involve erroneous math calculations (such as 0/0), or as a result of type coercion, either implicit or explicit.</p>\n<p>A common example is when you run parseInt on a string that starts with an alphabetical character. This isn’t exclusive to parseInt, as it also applies when using explicit coercion with Number(), or with the unary “+” operator.  </p>\n<h2 id=\"how-to-check-nan-in-javascript\" style=\"position:relative;\"><a href=\"#how-to-check-nan-in-javascript\" aria-label=\"how to check nan in javascript permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>How to check NaN in Javascript?</h2>\n<p>Before selecting a method for checking for NaN, <em>how should you</em> <strong><em>not</em></strong> <em>check for NaN?</em></p>\n<p>NaN is a bizarre value in JavaScript, as it does not equal itself when compared, either with the loose equality (==) or strict equality (===) operator. NaN is the only value in the entire language which behaves in this manner with regards to comparisons.  </p>\n<p>For example, if parseInt(“a”) returns NaN, then parseInt(“a”) === NaN will return false. This may seem strange, but it makes perfect sense after thinking about what NaN really is.  </p>\n<p>NaN doesn’t tell you what something is, it tells you what it <strong>isn’t</strong>.  </p>\n<p>These two different strings being passed to parseInt() will both return NaN.</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"javascript\" data-index=\"0\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk11\">parseInt</span><span class=\"mtk1\">(“</span><span class=\"mtk12\">abc</span><span class=\"mtk1\">”) </span><span class=\"mtk3\">// NaN</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk11\">parseInt</span><span class=\"mtk1\">(“</span><span class=\"mtk12\">def</span><span class=\"mtk1\">”) </span><span class=\"mtk3\">// NaN</span></span></code></pre>\n<p>Both statements return NaN, but are they <em>really</em> the same? Maybe, but it certainly makes sense why JavaScript would disagree, given that they are derived from different string arguments.  </p>\n<p>Here are a few examples of strict inequality comparisons, which demonstrate the inconsistency of NaN.</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"javascript\" data-index=\"1\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk7\">2</span><span class=\"mtk1\"> !== </span><span class=\"mtk7\">2</span><span class=\"mtk1\"> </span><span class=\"mtk3\">// false</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk4\">true</span><span class=\"mtk1\"> !== </span><span class=\"mtk4\">true</span><span class=\"mtk1\"> </span><span class=\"mtk3\">// false</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">“</span><span class=\"mtk12\">abc</span><span class=\"mtk1\">” !== “</span><span class=\"mtk12\">abc</span><span class=\"mtk1\">” </span><span class=\"mtk3\">// false</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">...</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk4\">NaN</span><span class=\"mtk1\"> !== </span><span class=\"mtk4\">NaN</span><span class=\"mtk1\"> </span><span class=\"mtk3\">// true</span></span></code></pre>\n<h3 id=\"method-1-isnan-or-numberisnan\" style=\"position:relative;\"><a href=\"#method-1-isnan-or-numberisnan\" aria-label=\"method 1 isnan or numberisnan permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Method 1: isNaN or Number.isNaN</h3>\n<p>JavaScript has a built-in method, appropriately named “isNaN,” which checks for NaN. There is a newer function called Number.isNaN, which is included in the ES2015 spec.  </p>\n<p>The difference between isNaN and Number.isNaN is that isNaN coerces the argument into a number type. To avoid complicated and unexpected outcomes, it is often advised to use the newer, more robust Number.isNaN to avoid these side effects.</p>\n<p>Number.isNaN does not perform any forcible type conversion, so it simply returns the boolean based on the parameter.  </p>\n<p>Here is an example of the difference between the two methods:  </p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"javascript\" data-index=\"2\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk11\">isNaN</span><span class=\"mtk1\">(</span><span class=\"mtk4\">undefined</span><span class=\"mtk1\">) </span><span class=\"mtk3\">// true</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk10\">Number</span><span class=\"mtk1\">.</span><span class=\"mtk11\">isNaN</span><span class=\"mtk1\">(</span><span class=\"mtk4\">undefined</span><span class=\"mtk1\">) </span><span class=\"mtk3\">// false</span></span></code></pre>\n<p>isNaN, when passed undefined, returns true because undefined becomes NaN after number coercion. You can test this yourself by running Number(undefined). You will find that it returns NaN.  </p>\n<p>Number.isNaN, on the other hand, returns false. This is because no coercion takes place, and undefined is <em>not</em> NaN, it is simply undefined.  </p>\n<p>It is also important to note that Number.isNaN is a newer (ES2015) <a href=\"https://www.loginradius.com/blog/engineering/16-javascript-hacks-for-optimization/\">method in JavaScript</a>, so browser support for Number.isNaN is not as stable as isNaN, which has been around since ES1 (1997).  </p>\n<h3 id=\"method-2-objectis\" style=\"position:relative;\"><a href=\"#method-2-objectis\" aria-label=\"method 2 objectis permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Method 2: Object.is</h3>\n<p><code>Object.is</code> is a JavaScript method which checks for sameness. It generally performs the same evaluations as a strict equality operator (===), although it treats NaN differently from strict equality.  </p>\n<p><code>Object.is(0, -0)</code> will return false, while 0 === -0 will return true. Comparisons of 0 and -0 differ, as do comparisons of NaN. This concept is called “Same-value-zero equality.”  </p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"javascript\" data-index=\"3\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk4\">NaN</span><span class=\"mtk1\"> === </span><span class=\"mtk4\">NaN</span><span class=\"mtk1\"> </span><span class=\"mtk3\">// false</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk10\">Object</span><span class=\"mtk1\">.</span><span class=\"mtk11\">is</span><span class=\"mtk1\">(</span><span class=\"mtk4\">NaN</span><span class=\"mtk1\">, </span><span class=\"mtk4\">NaN</span><span class=\"mtk1\">) </span><span class=\"mtk3\">// true</span></span></code></pre>\n<p><code>Object.is(NaN, NaN)</code> will in fact return true, while we already know that NaN === NaN returns false. That makes this yet another valid way to check if something is not a number.</p>\n<h2 id=\"conclusion\" style=\"position:relative;\"><a href=\"#conclusion\" aria-label=\"conclusion permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Conclusion</h2>\n<p>Between the given methods of checking for NaN, the most common is to use the global isNaN function, or the ES2015 Number.isNaN method. While method #2 is valid, most people will typically use isNaN or Number.isNaN, which were created specifically for checking for NaN.</p>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n  }\n  \n  .grvsc-code {\n    display: inline-block;\n    min-width: 100%;\n  }\n  \n  .grvsc-line {\n    display: inline-block;\n    box-sizing: border-box;\n    width: 100%;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-line-highlighted {\n    background-color: var(--grvsc-line-highlighted-background-color, transparent);\n    box-shadow: inset var(--grvsc-line-highlighted-border-width, 4px) 0 0 0 var(--grvsc-line-highlighted-border-color, transparent);\n  }\n  \n  .dark-default-dark {\n    background-color: #1E1E1E;\n    color: #D4D4D4;\n  }\n  .dark-default-dark .mtk11 { color: #DCDCAA; }\n  .dark-default-dark .mtk1 { color: #D4D4D4; }\n  .dark-default-dark .mtk12 { color: #9CDCFE; }\n  .dark-default-dark .mtk3 { color: #6A9955; }\n  .dark-default-dark .mtk7 { color: #B5CEA8; }\n  .dark-default-dark .mtk4 { color: #569CD6; }\n  .dark-default-dark .mtk10 { color: #4EC9B0; }\n</style>","headings":[{"value":"What is NaN in Javascript?","depth":2},{"value":"Values in NaN Javascript","depth":2},{"value":"How to check NaN in Javascript?","depth":2},{"value":"Method 1: isNaN or Number.isNaN","depth":3},{"value":"Method 2: Object.is","depth":3},{"value":"Conclusion","depth":2}],"fields":{"slug":"/engineering/checking-for-nan-in-javascript/"},"frontmatter":{"metatitle":null,"metadescription":null,"description":"In this guide you'll learn about JavasScript NaN, how to verify whether a value is NaN, and how to manage NaN effectively.","title":"NaN in JavaScript: An Essential Guide","canonical":null,"date":"November 22, 2019","updated_date":null,"tags":["JavaScript"],"coverImage":{"childImageSharp":{"fluid":{"aspectRatio":1.3333333333333333,"src":"/static/64b659badd7a09c3d955b9c49a7f271f/f1752/numbers-1.webp","srcSet":"/static/64b659badd7a09c3d955b9c49a7f271f/1c9b5/numbers-1.webp 200w,\n/static/64b659badd7a09c3d955b9c49a7f271f/f1752/numbers-1.webp 400w","sizes":"(max-width: 400px) 100vw, 400px"}}},"author":{"id":"Greg Sakai","github":null,"bio":"Greg Sakai is a Software Developer at LoginRadius. He is currently studying Computer Science part time, and he loves all things JavaScript. In his spare time, he likes doing homework.","avatar":null}}}},"pageContext":{"id":"0f63d421-ce01-5020-8b12-991083fd5274","fields__slug":"/engineering/checking-for-nan-in-javascript/","__params":{"fields__slug":"engineering"}}},"staticQueryHashes":["1171199041","1384082988","1711371485","1753898100","2100481360","229320306","23180105","528864852"]}