compare string with numbers in javascript

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Matt

    #1

    compare string with numbers in javascript

    if (123 > 33) will return true

    and

    if ("123" > 33) will return true

    So my question is, if the above behaviors are the same?? If string is
    a number, and
    compare with another number, it will be the same behavior as compare 2
    numbers?

    In this case, it is comparing 2 strings that are numbers, so they are
    string comparisons here.
    correct?

    if ("123" > "33") will return true

    In this case, "33a" is not a number, that's why when it compare with
    another number, it
    always return false. correct?

    if ("33a" > 33) will return false

    please advise. thanks!!
  • Lasse Reichstein Nielsen

    #2
    Re: compare string with numbers in javascript

    jrefactors@hotm ail.com (Matt) writes:
    [color=blue]
    > if (123 > 33) will return true
    >
    > and
    >
    > if ("123" > 33) will return true[/color]

    Correct.
    [color=blue]
    > So my question is, if the above behaviors are the same?? If string
    > is a number, and compare with another number, it will be the same
    > behavior as compare 2 numbers?[/color]

    Yes. The "greater than" comparison operator, when given a number and
    another argument, will convert the other argument to a number. The
    conversion is the same as what the Number function does.
    [color=blue]
    > In this case, it is comparing 2 strings that are numbers, so they are
    > string comparisons here.
    > correct?[/color]

    Correct. If *both* arguments are strings, the comparsion is lexical
    comparison.
    [color=blue]
    > if ("123" > "33") will return true[/color]

    The result of ("123" > "33") is false, since the first character of
    the first string is smaller than the first character of the second
    string.
    [color=blue]
    > In this case, "33a" is not a number, that's why when it compare with
    > another number, it always return false. correct?[/color]

    Correct, because (Number("33a")) yields NaN, and (NaN > 33) is false.

    /L
    --
    Lasse Reichstein Nielsen - lrn@hotpop.com
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
    'Faith without judgement merely degrades the spirit divine.'

    Comment

    • Grant Wagner

      #3
      Re: compare string with numbers in javascript

      Lasse Reichstein Nielsen wrote:
      [color=blue]
      > Correct, because (Number("33a")) yields NaN, and (NaN > 33) is false.[/color]

      Actually, (Number.NaN < 33) is false as well. As is (Number.NaN == Number.NaN).

      (Number.NaN != Number.NaN) is true, which goes against common sense, but it's
      the documented behaviour:

      <url:

      />
      <url: http://msdn.microsoft.com/library/en...onannumber.asp
      />

      --
      Grant Wagner <gwagner@agrico reunited.com>
      comp.lang.javas cript FAQ - http://jibbering.com/faq

      Comment

      • Lasse Reichstein Nielsen

        #4
        Re: compare string with numbers in javascript

        Grant Wagner <gwagner@agrico reunited.com> writes:
        [color=blue]
        > Actually, (Number.NaN < 33) is false as well. As is (Number.NaN ==
        > Number.NaN).
        >
        > (Number.NaN != Number.NaN) is true, which goes against common sense,[/color]

        I'd say that (NaN == NaN) being false is what goes against common sense.
        From that, I find it entirely logical that
        (NaN != NaN) is the same as !(NaN == NaN)

        /L
        --
        Lasse Reichstein Nielsen - lrn@hotpop.com
        DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
        'Faith without judgement merely degrades the spirit divine.'

        Comment

        Working...