6 Replies Latest reply on Sep 20, 2006 11:10 PM by Hangring

    What happened?

    Hangring
      if ("") {
      trace("\"\"");
      }
      Or
      if ('') {
      trace("''");
      }
      output "" or ''.
      But, 'while' is all right!
      I don't know what happended?
        • 1. Re: What happened?
          Level 7
          Hangring,

          > if ("") {
          > trace("\"\"");
          > }
          > Or
          > if ('') {
          > trace("''");
          > }
          > output "" or ''.

          Okay, I'm with you so far.

          > But, 'while' is all right!
          > I don't know what happended?

          That's where I lose you. What are you asking?


          David Stiller
          Adobe Community Expert
          Dev blog, http://www.quip.net/blog/
          "Luck is the residue of good design."


          • 2. Re: What happened?
            Level 7
            > if ("") {

            Why would you write such a thing?
            --
            Jeckyl


            • 3. Re: What happened?
              NSurveyor Level 2
              I thinkn the OP is asking why "" evaluates to true in an if statement, but not in a while statement. Example:

              if(''){
              trace('indeed');
              }

              the trace WILL occur. But if you do:

              while(''){
              trace('indeedy');
              }

              nothing happens.

              Well to me, it looks like some compiler sort of thing. When it sees if('') it simply removes it, but keeps the code that would be in the braces. But if you did:

              x = '';
              if(x){
              trace("hi");
              }

              hi would not be traced. So, if you don't have if('') it will evaluate x using the Boolean conversion function, and thus Boolean('') = false
              • 4. Re: What happened?
                kglad Adobe Community Professional & MVP
                lol, i'm with jeckyl on this one.
                • 5. Re: What happened?
                  ggshow Level 2
                  ok, let me guess... what Hangring want to know is:
                  empty string / double quot ("") return TRUE or FALSE

                  // if convert "" to boolean
                  a = Boolean("");
                  trace(a); //it return false

                  while ("") {
                  // statement here doesn't works, it is normal
                  }

                  if ("") {
                  // statement here works, why?
                  }

                  OK, the point is...

                  for IF and WHILE statements, the condition you can put inside the () supposed to be Boolean, but not a string :-)
                  • 6. What happened?
                    Hangring Level 1
                    Yes. When i show all that the value return 'false', follow:
                    ------
                    1) false or !true
                    2) undefined
                    3) null
                    4) "" or ''
                    5) 0 or 0.0
                    6) undefined variable
                    ------
                    When i write:

                    if (undefined) {
                    // don't works
                    }
                    Or
                    var a = undefined;
                    if (a) {
                    // don't works
                    }

                    And so on, then:
                    var a = "";
                    if (a) {
                    // don't works
                    }

                    But:
                    if ("") {
                    // works
                    }


                    In JavaScript:
                    var a = "";
                    if (a) {
                    // don't works
                    }
                    Or
                    if ("") {
                    // don't works
                    }

                    My question is that the "" or '' what meant that in if statement.


                    Thanks.