easy debug question?

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

    easy debug question?

    I know this has to be easy, but I can't seem to figure it out. I'm debuggin' someone elses code...

    x="hello";
    +x+"world"

    ....and I don't get it! What does the "+x+"world" do? If I alert(x) the only thing that comes out is "hello". I also wondered why it doesn't error without the ";" at the end of the line.

    I appreciate the help...

    -Bruce Duncan
    **** feeling dumb today...just wait till tomorrow....*** *
  • kaeli

    #2
    Re: easy debug question?

    In article <10b958im5clch4 f@corp.supernew s.com>,
    bruce~w~duncan@ ~hotmail.com enlightened us with...[color=blue]
    > I know this has to be easy, but I can't seem to figure it out. I'm debuggin' someone elses code...
    >
    > x="hello";
    > +x+"world"
    >
    > ...and I don't get it! What does the "+x+"world" do?
    > If I alert(x) the only thing that comes out is "hello".
    > I also wondered why it doesn't error without the ";" at the end of the line.
    >
    > I appreciate the help...
    >[/color]


    Um, I don't think the semi-colon should be there. I think it was
    supposed to be one line. Or the second line should have been
    x=x+" world";
    That code doesn't look like it is valid. Note that browsers don't
    necessarily show you errors.

    Semi-colons are not required at the end of the line in JS; they are
    recommended for clarity if the end of the line is also the end of the
    statement.
    (the above is a bit simplified)

    --
    --
    ~kaeli~
    You feel stuck with your debt if you can't budge it.



    Comment

    • Bruce Duncan

      #3
      Re: easy debug question?

      "kaeli" <tiny_one@NOSPA M.comcast.net> wrote in message
      news:MPG.1b1e60 14bdaf3d25989e9 4@nntp.lucent.c om...[color=blue]
      > In article <10b958im5clch4 f@corp.supernew s.com>,
      > bruce~w~duncan@ ~hotmail.com enlightened us with...[color=green]
      > > I know this has to be easy, but I can't seem to figure it out. I'm[/color][/color]
      debuggin' someone elses code...[color=blue][color=green]
      > >
      > > x="hello";
      > > +x+"world"
      > >
      > > ...and I don't get it! What does the "+x+"world" do?
      > > If I alert(x) the only thing that comes out is "hello".
      > > I also wondered why it doesn't error without the ";" at the end of the[/color][/color]
      line.[color=blue][color=green]
      > >
      > > I appreciate the help...
      > >[/color]
      >
      >
      > Um, I don't think the semi-colon should be there. I think it was
      > supposed to be one line. Or the second line should have been
      > x=x+" world";
      > That code doesn't look like it is valid. Note that browsers don't
      > necessarily show you errors.
      >
      > Semi-colons are not required at the end of the line in JS; they are
      > recommended for clarity if the end of the line is also the end of the
      > statement.
      > (the above is a bit simplified)
      >
      > --
      > --
      > ~kaeli~
      > You feel stuck with your debt if you can't budge it.
      > http://www.ipwebdesign.net/wildAtHeart
      > http://www.ipwebdesign.net/kaelisSpace[/color]

      I believe you are correct Kaeli!
      I was hoping that it wasn't some wierd way of assigning vars that I just
      hadn't seen before. My mind isn't working so great this morning...thank s
      for the response...it helped open my eyes a bit.

      -Bruce Duncan



      Comment

      • Grant Wagner

        #4
        Re: easy debug question?

        Bruce Duncan wrote:
        [color=blue]
        > I know this has to be easy, but I can't seem to figure it out.
        > I'm debuggin' someone elses code... x="hello";
        > +x+"world"...an d I don't get it! What does the "+x+"world"
        > do? If I alert(x) the only thing that comes out is "hello". I
        > also wondered why it doesn't error without the ";" at the end
        > of the line. I appreciate the help... -Bruce Duncan**** feeling
        > dumb today...just wait till tomorrow....*** *[/color]

        The +x + "world" doesn't do anything. Well, actually, it applies
        the unary operator to the value of x (returning zero). toString()
        is implicitly called on that zero and it is concatentated to the
        word "world". The result is a string "0world". However, that
        value is never assigned to anything, so it is discarded almost
        immediately.

        Semi-colons are only required in Javascript when multiple
        statements appear on the same line, however, it's a good habit to
        put them at the end of every non-block.

        As for why this isn't doing what you want, I think what you want
        is:

        var x = "hello";
        x = x + " world"; // notice x + " world" is assigned back to x
        // you can also use x += " world";
        alert(x);

        --
        | Grant Wagner <gwagner@agrico reunited.com>

        * Client-side Javascript and Netscape 4 DOM Reference available
        at:
        *


        * Internet Explorer DOM Reference available at:
        *
        Learn with interactive lessons and technical documentation, earn professional development hours and certifications, and connect with the community.


        * Netscape 6/7 DOM Reference available at:
        * http://www.mozilla.org/docs/dom/domref/
        * Tips for upgrading JavaScript for Netscape 7 / Mozilla
        * http://www.mozilla.org/docs/web-deve...upgrade_2.html


        Comment

        Working...