06.07.2017 Views

Mastering JavaScript

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

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

Before you judge the validity of these arguments, you need to understand what is<br />

affected by ASI. The following statements are affected by ASI:<br />

• An empty statement<br />

• A var statement<br />

• An expression statement<br />

• A do-while statement<br />

• A continue statement<br />

• A break statement<br />

• A return statement<br />

• A throw statement<br />

Chapter 1<br />

The idea behind ASI is to make semicolons optional at the end of a line. This way,<br />

ASI helps the parser to determine when a statement ends. Normally, it ends with a<br />

semicolon. ASI dictates that a statement also ends in the following cases:<br />

• A line terminator (for example, a newline) is followed by an illegal token<br />

• A closing brace is encountered<br />

• The end of the file has been reached<br />

Let's see the following example:<br />

if (a < 1) a = 1 console.log(a)<br />

The console token is illegal after 1 and triggers ASI as follows:<br />

if (a < 1) a = 1; console.log(a);<br />

In the following code, the statement inside the braces is not terminated by a<br />

semicolon:<br />

function add(a,b) { return a+b }<br />

ASI creates a syntactically correct version of the preceding code:<br />

function add(a,b) { return a+b; }<br />

[ 31 ]<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!