By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,970 Members | 1,669 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,970 IT Pros & Developers. It's quick & easy.

easy debug question?

P: n/a
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....****
Jul 23 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
In article <10*************@corp.supernews.com>,
bruce~w~duncan@~hotmail.com enlightened us with...
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...

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

Jul 23 '05 #2

P: n/a
"kaeli" <ti******@NOSPAM.comcast.net> wrote in message
news:MP************************@nntp.lucent.com...
In article <10*************@corp.supernews.com>,
bruce~w~duncan@~hotmail.com enlightened us with...
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...

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


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...thanks
for the response...it helped open my eyes a bit.

-Bruce Duncan

Jul 23 '05 #3

P: n/a
Bruce Duncan wrote:
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....****


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 <gw*****@agricoreunited.com>

* Client-side Javascript and Netscape 4 DOM Reference available
at:
*
http://devedge.netscape.com/library/...ce/frames.html

* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/a...ence_entry.asp

* 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
Jul 23 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.