number.isNaN - but why?

Hi,

I’m learning to think like a computer. According to the book ”Eloquent JavaScript”, this entails learning to think ”dumbly”. :slight_smile:

Is someone able to explain why I would write something like “unless theNumber is not-a-number, do this” as in the if statement below? Why wouldn’t I just write ”if it is a number, do this” and go from there?

let theNumber = Number(prompt(“Pick a number”));
if (!Number.isNaN(theNumber)) {
console.log("Your number is the square root of " +
theNumber * theNumber);
}console.log(theNumber);

:man_shrugging:

Are you asking the correct question?

Notice the

!

which is a

not

at the beginning.

1 Like

If you do an operation after which you might end up with NaN, then you’d want to check whether … you did end up with that, or an actual number. Number also has a isFinite method which might be what you’re after.

1 Like