From 836d9f017bf83b2d86c3f9c7ead747fcd2fe4537 Mon Sep 17 00:00:00 2001 From: Chalarangelo Date: Thu, 2 Dec 2021 13:23:01 +0200 Subject: [PATCH] Add JS NaN question --- .../javascript-value-not-equal-to-itself.md | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 blog_posts/javascript-value-not-equal-to-itself.md diff --git a/blog_posts/javascript-value-not-equal-to-itself.md b/blog_posts/javascript-value-not-equal-to-itself.md new file mode 100644 index 000000000..b87c4d8c1 --- /dev/null +++ b/blog_posts/javascript-value-not-equal-to-itself.md @@ -0,0 +1,27 @@ +--- +title: What is the only value not equal to itself in JavaScript? +type: question +tags: javascript,type,comparison +authors: chalarangelo +cover: blog_images/eagle.jpg +excerpt: Did you know there's a JavaScript value that's not equal to itself? +firstSeen: 2021-12-12T05:00:00-04:00 +--- + +`NaN` (Not-a-Number) is the only JavaScript value not equal to itself when comparing with any of the comparison operators. `NaN` is often the result of meaningless or invalid math computations, so it doesn't make sense for two `NaN` values to be considered equal. + +```js +const x = Math.sqrt(-1); // NaN +const y = 0 / 0; // NaN + +x === y; // false +x === NaN; // false + +Number.isNaN(x); // true +Number.isNaN(y); // true + +isNaN(x); // true +isNan('hello'); // true +``` + +You can check for `NaN` values using the `Number.isNaN()` function. Note that this is different from the original , global `isNaN()`. Their difference lies in the fact that `isNaN()` forcefully converts its argument to a number, whereas `Number.isNaN()` doesn't. This is why `Number.isNaN()` is considered more robust and preferable in most cases.