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