number.isNaN - but why?


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);


Are you asking the correct question?

Notice the


which is a


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