Testing for NaN

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Robert Mark Bram

    #1

    Testing for NaN

    Hi All!

    I know I can test like this:
    if (isNaN("blah"))

    But when I try this it doesn't work:
    if ("blah" == Number.NaN)

    What am I doing wrong?

    Thanks for any advice!

    Rob
    :)
    :>
    :]
  • Robert Mark Bram

    #2
    Re: Testing for NaN

    > I know I can test like this:[color=blue]
    > if (isNaN("blah"))
    >
    > But when I try this it doesn't work:
    > if ("blah" == Number.NaN)[/color]

    Correction. I meant to say:
    if (parseInt ("blah") == Number.NaN)
    [color=blue]
    > What am I doing wrong?
    >
    > Thanks for any advice![/color]

    Rob
    :)
    :>
    :]

    Comment

    • Laurent Bugnion, GalaSoft

      #3
      Re: Testing for NaN

      Hi,

      Robert Mark Bram wrote:
      [color=blue][color=green]
      >> I know I can test like this:
      >> if (isNaN("blah"))
      >>
      >> But when I try this it doesn't work:
      >> if ("blah" == Number.NaN)[/color]
      >
      >
      > Correction. I meant to say:
      > if (parseInt ("blah") == Number.NaN)
      >[color=green]
      >> What am I doing wrong?
      >>
      >> Thanks for any advice![/color]
      >
      >
      > Rob[/color]

      You didn't read the doc:

      <quote>
      NaN is always unequal to any other number, including NaN itself; you
      cannot check for the not-a-number value by comparing to Number.NaN. Use
      the isNaN function instead.
      </quote>

      From:
      <URL:
      http://devedge.netscap e.com/library/manuals/2000/javascript/1.5/reference/number.html#119 3302>

      HTH,

      Laurent
      --
      Laurent Bugnion, GalaSoft
      Software engineering: http://www.galasoft-LB.ch
      Private/Malaysia: http://mypage.bluewin.ch/lbugnion
      Support children in Calcutta: http://www.calcutta-espoir.ch

      Comment

      • Robert Mark Bram

        #4
        Re: Testing for NaN

        > You didn't read the doc:[color=blue]
        >
        > <quote>
        > NaN is always unequal to any other number, including NaN itself; you
        > cannot check for the not-a-number value by comparing to Number.NaN. Use
        > the isNaN function instead.
        > </quote>[/color]

        Oh. So much for that then. Thank you very much for pointing it out Laurent!

        Rob
        :)
        :>
        :]

        Comment

        • Dr John Stockton

          #5
          Re: Testing for NaN

          JRS: In article <opr021sydhyneq vgrmb@news.optu snet.com.au>, seen in
          news:comp.lang. javascript, Robert Mark Bram <relaxedrob@rem ove.this.optu
          snet.com.au> posted at Thu, 1 Jan 2004 17:04:00 :-
          [color=blue]
          >I know I can test like this:
          > if (isNaN("blah"))
          >
          >But when I try this it doesn't work:
          > if ("blah" == Number.NaN)
          >
          >What am I doing wrong?[/color]

          Using the second method instead of the first. Deciding that you know
          what the second should do without reference to adequate documentation.


          A NaN is not equal to anything at all, not even to another NaN generated
          the same way, or to a copy of itself.


          One use of this, BTW, is in dealing with a user-given range. If the
          test is carefully arranged, it can give the desired result with NaN :

          if (!(Y2-Y1<26) || Y1>Y2) { // does NaN
          DynWrite(Div, "<b>Bad Range<\/b>") ; return }

          --
          © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
          <URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang. javascript
          <URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
          <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

          Comment

          Working...