Automatic Semicolon Insertion (ASI)

The main target of this semicolon insertion is to make semicolon optional at the end of the line. It is invoked by the JavaScript to insert semicolon at the end of the line for you and internally the things are handled differently.

ASI parser identifies when the statement ends. Basically, the line ends by semicolon and it also identifies that the statement also ends based on these conditions :

1. A line terminator followed by illegal token.
2. If a closing brace is encountered.
3. If the end of file is reached.

ASI vs illegal token

The following code has an illegal token.

if( i < 1 ) i = 5
console.log(i)

Here, this comes under the section of illegal statement and it triggers ASI, then it adds the semicolonn at the end of the line as shown below :

if( i < 1 ) i = 5;
console.log(i);

ASI vs Closing brace

In the following code, the statement inside the braces is not ended with the semi colon.

function mul(a,b){ return a*b }

Now, the ASI is triggered and it creates a correct version of the preceding code :

function mul(a,b){ return a*b; }





No comments:

Post a Comment

Overview of AngularJS

AngularJS is an open source web application framework. It is now maintained by Google . The most of the code can be eliminated by using th...