Member-only story
Automatic Semicolon Insertion in Javascript
2 min readJan 12, 2018
Do we really need to have Automatic Semicolon Insertion
in Javascript?
Well, what is ASI?
When parsing the Javascript, the parser will add semicolons at the end of every statement.
// for example if you define
let a = 5
let b = a + 1// The compiler will insert an automatic semicolon.
let a = 5;
let b = a + 1;
Isn’t this normal, why do we even bother about this? Well it is normal in almost all the places.
But not on few corner cases. Like below,
function return_one() { return 1}// the above code will return 1 function return_undefined() {return
1}// but this will return undefined.// why?// the parser parses like thisfunction return_undefined() { return;
1;}
As you can see, you will see the error only on the runtime. As newer functionalities are added into the language, corner cases tend to grow. (places where the ASI will cause an unexpected behavior)
More corner cases lies around,
- Template string
- unary operator
+
& -