Skip to content

About isNaN #148

Closed
Closed

Description

@xfq

In the "Conditional execution" section of Chapter 2:

var theNumber = prompt("Pick a number", "");
if (!isNaN(theNumber))
  alert("Your number is the square root of " +
        theNumber * theNumber);

With this modification, if you enter “cheese”, no output will be shown.

The string "cheese" is surely "not a number". But "not a number" has a specific meaning for numbers represented as IEEE-754 floating-point values. This is confusing.

I suggest replacing the isNaN function with the typeof operator (typeof "cheese" !== 'number'). Or at least use the Number.isNaN() function in ES6:

isNaN("cheese") //true
Number.isNaN("cheese") //false

Activity

  1. marijnh commented on Dec 14, 2014

    @marijnh
    Owner

    I am not trying to use isNaN as a typeof equivalent here (after all, prompt will never return a number). Rather, I'm testing whether the string holds a valid number. For some reason, the explicit Number conversion that I used to have here got dropped in an apparently unrelated patch (d7c72a4). Not sure what I was doing there, but attached patch restores it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions