# Check if number is NaN

**URL:** <https://forum.makecode.com/t/check-if-number-is-nan/4634>\
**Category:** Help\
**Created:** [November 25, 2020, 2:46am UTC](https://forum.makecode.com/t/check-if-number-is-nan/4634 "2020-11-25T02:46:55Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![UnsignedArduino](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/unsignedarduino/32/592_2.png) [@UnsignedArduino](https://forum.makecode.com/u/UnsignedArduino)\
**Post date:** [November 25, 2020, 2:46am UTC](https://forum.makecode.com/t/check-if-number-is-nan/4634/1 "2020-11-25T02:46:55Z")

</div>

When you input `.` or `-` into `ask for number`, the result is `NaN`, which evaluates into `0`.

How can I check for `NaN` in blocks?

---

<div class="post-metadata">

**Author:** ![GameGod](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/gamegod/32/2756_2.png) [@GameGod](https://forum.makecode.com/u/GameGod)\
**Post date:** [November 25, 2020, 2:55am UTC](https://forum.makecode.com/t/check-if-number-is-nan/4634/2 "2020-11-25T02:55:02Z")

</div>

If () = 0 (for numbers)  
If () is empty (for text)  
If length of [] = 0 (for arrays)  
if () is true/false (for booleans)

---

<div class="post-metadata">

**Author:** ![UnsignedArduino](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/unsignedarduino/32/592_2.png) [@UnsignedArduino](https://forum.makecode.com/u/UnsignedArduino)\
**Post date:** [November 25, 2020, 3:02am UTC](https://forum.makecode.com/t/check-if-number-is-nan/4634/3 "2020-11-25T03:02:24Z")

</div>

Sorry, I don’t get what you are trying to say. Can you please elaborate?

---

<div class="post-metadata">

**Author:** ![UnsignedArduino](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/unsignedarduino/32/592_2.png) [@UnsignedArduino](https://forum.makecode.com/u/UnsignedArduino)\
**Post date:** [November 25, 2020, 3:02am UTC](https://forum.makecode.com/t/check-if-number-is-nan/4634/4 "2020-11-25T03:02:26Z")

</div>

Haha, did some “Stack Overflowing” and found this:

> <https://stackoverflow.com/questions/30314447/how-do-you-test-for-nan-in-javascript/35912757#35912757>

Which contains this code snippet:

```javascript
if(x !== x) {
    console.info('x is NaN.');
}
else {
    console.info('x is NOT a NaN.');
}

```

So basically just check if the variable is not equal to itself because `NaN` isn’t even equal to itself.

```typescript
let value = NaN
if (value !== value) {
    // value is NaN
} else {
    // value is not NaN
}

```

---

<div class="post-metadata">

**Author:** ![UnsignedArduino](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/unsignedarduino/32/592_2.png) [@UnsignedArduino](https://forum.makecode.com/u/UnsignedArduino)\
**Post date:** [March 2, 2021, 10:09pm UTC](https://forum.makecode.com/t/check-if-number-is-nan/4634/5 "2021-03-02T22:09:26Z")

</div>

@jwunderl I swear I saw `null` != `null` somewhere 😅 I need to spend more time with JS…

---

<div class="post-metadata">

**Author:** ![jwunderl](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/jwunderl/32/5308_2.png) [@jwunderl](https://forum.makecode.com/u/jwunderl)\
**Post date:** [March 2, 2021, 10:31pm UTC](https://forum.makecode.com/t/check-if-number-is-nan/4634/6 "2021-03-02T22:31:28Z")

</div>

Yeah, NaN is the weird one as it’s defined to be not equal to itself by the IEEE 754 floating point number spec; for reference there’s also `Number.isNaN(1)` and `isNaN(3)` (which are effectively identical in ts as the difference is one coerces to number and one does not), but no blocks for those~

---

<div class="post-metadata">

**Author:** ![WoofWoof](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/woofwoof/32/32523_2.png) [@WoofWoof](https://forum.makecode.com/u/WoofWoof)\
**Post date:** [November 30, 2023, 5:17pm UTC](https://forum.makecode.com/t/check-if-number-is-nan/4634/7 "2023-11-30T17:17:05Z")

</div>

This doesn’t work. NaN is not equal to anything, not even 0. It might work for the other ones, but not for numbers. X != X works for some reason though.

---

<div class="post-metadata">

**Author:** ![Sarge](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/sarge/32/19590_2.png) [@Sarge](https://forum.makecode.com/u/Sarge)\
**Post date:** [November 30, 2023, 6:24pm UTC](https://forum.makecode.com/t/check-if-number-is-nan/4634/8 "2023-11-30T18:24:59Z")

</div>

This is a certified JavaScript moment
