Check if number is NaN

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

How can I check for NaN in blocks?

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

1 Like

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

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

Which contains this code snippet:

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.

let value = NaN
if (value !== value) {
    // value is NaN
} else {
    // value is not NaN
}
1 Like

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

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~

1 Like