11.12.2012 Views

JavaScript 2.0-The Complete Reference, Second ... - freecodingtutorial

JavaScript 2.0-The Complete Reference, Second ... - freecodingtutorial

JavaScript 2.0-The Complete Reference, Second ... - freecodingtutorial

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

y = y - 1<br />

is fine. But if you make it<br />

x = x + 1 y = y - 1<br />

you will throw an error. Also, if you break a statement up into multiple lines you might cause a<br />

problem. <strong>The</strong> classic example is the return statement. Because the argument to return is<br />

optional, placing return and its argument on separate lines causes the return to execute<br />

without the argument. For example,<br />

return<br />

x<br />

is treated as<br />

return;<br />

x;<br />

rather than what was probably intended:<br />

return x;<br />

For this reason and others, such as readability of your code, terminating statements with a line<br />

break and relying on implicit semicolon insertion is not only poor programming style, but a bad<br />

idea and should be avoided.<br />

Blocks<br />

Curly braces ({ }) are used to group a series of consecutive statements together. Doing so<br />

creates one large statement, so a block of statements enclosed in curly braces can be used<br />

anywhere in <strong>JavaScript</strong> that a single statement could. For example, a statement is expected as<br />

the body of an if conditional:<br />

if (some condition)<br />

do something;<br />

Because a block is treated as a single statement, you could also write<br />

if (some condition)<br />

{<br />

}<br />

do something;<br />

do something else;<br />

...

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!