Why is 0 == "" in JavaScript?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • gappodi@gmail.com

    Why is 0 == "" in JavaScript?

    Why is
    0 == "" in JavaScript

    Since "A" + 0 != "A" + ""

    thank you.
    Gap
  • Evertjan.

    #2
    Re: Why is 0 == "" in JavaScript?

    wrote on 21 mrt 2008 in comp.lang.javas cript:
    Why is
    0 == "" in JavaScript
    because

    var result = 0 == "";

    is not the same as

    var result = 0 === "";

    ==========

    With:

    var result = 0 == "";

    both being near equivalent to false,
    and


    var result = false == false;

    results in true.


    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)

    Comment

    • Stevo

      #3
      Re: Why is 0 == "" in JavaScript?

      gappodi@gmail.c om wrote:
      Why is
      0 == "" in JavaScript
      >
      Since "A" + 0 != "A" + ""
      Well, "A"+0 is "A0".

      As for 0 == "", that's all done to automatic type conversions and the
      fact that javascript considers false, 0, "", undefined all as false, and
      the rest as true. So 0 is false and "" is false, therefore 0==""

      Comment

      • Douglas Crockford

        #4
        Re: Why is 0 == "" in JavaScript?

        gappodi@gmail.c om wrote:
        Why is
        0 == "" in JavaScript
        >
        Since "A" + 0 != "A" + ""
        Mistakes were made.

        Comment

        • Rick Merrill

          #5
          Re: Why is 0 == "" in JavaScript?

          Thomas 'PointedEars' Lahn wrote:
          gappodi@gmail.c om wrote:
          >Why is
          >0 == "" in JavaScript
          >
          Steps of evaluation (cf. ECMAScript Ed. 3 Final, sections 11.9.1 to 11.9.3):
          >
          0 == ""
          +0 == +0
          true
          >
          >Since "A" + 0 != "A" + ""
          >
          "A" + 0 != "A" + ""
          !("A" + +0 == "A" + "")
          !("A" + "0" == "A" + "")
          !("A0" == "A")
          !(false)
          true
          >
          >
          PointedEars
          i think i see your answer to the OP is correct,
          but how do you explain the apparent contradiction
          0 == ""
          AND
          0 == "0"

          ?

          Comment

          • Thomas 'PointedEars' Lahn

            #6
            Re: Why is 0 == "" in JavaScript?

            Rick Merrill wrote:
            Thomas 'PointedEars' Lahn wrote:
            >gappodi@gmail.c om wrote:
            >>Why is
            >>0 == "" in JavaScript
            >Steps of evaluation (cf. ECMAScript Ed. 3 Final, sections 11.9.1 to 11.9.3):
            >>
            > 0 == ""
            > +0 == +0
            > true
            >>
            >>Since "A" + 0 != "A" + ""
            > "A" + 0 != "A" + ""
            > !("A" + +0 == "A" + "")
            > !("A" + "0" == "A" + "")
            > !("A0" == "A")
            > !(false)
            > true
            >[...]
            Please trim your quotes, do not quote signatures unless referring directly
            to them.
            i think i see your answer to the OP is correct,
            but how do you explain the apparent contradiction
            0 == ""
            AND
            0 == "0"
            What is perceived as a contradiction by the OP and you here is ignoring that
            string concatenation also involves type conversion.


            PointedEars
            --
            var bugRiddenCrashP ronePieceOfJunk = (
            navigator.userA gent.indexOf('M SIE 5') != -1
            && navigator.userA gent.indexOf('M ac') != -1
            ) // Plone, register_functi on.js:16

            Comment

            • Joost Diepenmaat

              #7
              Re: Why is 0 == "" in JavaScript?

              Rick Merrill <rick0.merrill@ gmail.comwrites :
              i think i see your answer to the OP is correct,
              but how do you explain the apparent contradiction
              0 == ""
              AND
              0 == "0"
              That's not a contradiction, that's just type conversion.

              See section 11.8.5 of the emacascript 262 specs:

              The Abstract Equality Comparison Algorithm

              The comparison x == y, where x and y are values, produces true or
              false. Such a comparison is performed as follows:

              1. If Type(x) is different from Type(y), go to step 14.

              [ snip ]

              14. If x is null and y is undefined, return true.
              15. If x is undefined and y is null, return true.
              16. If Type(x) is Number and Type(y) is String,
              return the result of the comparison x == ToNumber(y).
              ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^
              [ snip ]

              ToNumber("0") and ToNumber("") both evaluate to 0.


              --
              Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/

              Comment

              • Thomas 'PointedEars' Lahn

                #8
                Re: Why is 0 == &quot;&quot; in JavaScript?

                Joost Diepenmaat wrote:
                See section 11.8.5 of the emacascript 262 specs:
                It is ECMA-262 or ECMAScript, not both. Otherwise you are correct.


                PointedEars
                --
                var bugRiddenCrashP ronePieceOfJunk = (
                navigator.userA gent.indexOf('M SIE 5') != -1
                && navigator.userA gent.indexOf('M ac') != -1
                ) // Plone, register_functi on.js:16

                Comment

                Working...