compare string with numbers in javascript

Discussion in 'Javascript' started by Matt, Sep 12, 2004.

  1. Matt

    Matt Guest

    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!!
    Matt, Sep 12, 2004
    #1
    1. Advertising

  2. (Matt) writes:

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


    Correct.

    > 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?


    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.

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


    Correct. If *both* arguments are strings, the comparsion is lexical
    comparison.

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


    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.

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


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

    /L
    --
    Lasse Reichstein Nielsen -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
    Lasse Reichstein Nielsen, Sep 12, 2004
    #2
    1. Advertising

  3. Matt

    Grant Wagner Guest

    Lasse Reichstein Nielsen wrote:

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


    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:
    http://devedge.netscape.com/library/manuals/2000/javascript/1.3/reference/number.html#1193302
    />
    <url: http://msdn.microsoft.com/library/en-us/jscript7/html/jspronannumber.asp
    />

    --
    Grant Wagner <>
    comp.lang.javascript FAQ - http://jibbering.com/faq
    Grant Wagner, Sep 13, 2004
    #3
  4. Grant Wagner <> writes:

    > 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,


    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 -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
    Lasse Reichstein Nielsen, Sep 13, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    10
    Views:
    775
  2. Replies:
    10
    Views:
    752
    Roland Pibinger
    Jan 24, 2007
  3. Justin C
    Replies:
    8
    Views:
    128
  4. Replies:
    11
    Views:
    276
  5. Replies:
    2
    Views:
    421
Loading...

Share This Page