Automatic Semicolon Insertion

  • When building scripts, Javascript occasionally enables us to avoid using semicolons, albeit it still detects them without any issues. The intriguing part is that, when learned, this conduct formalises misunderstanding. The documentation that explains this kind of language behaviour is likewise written in a little bit of a technical style.
  • I’ll attempt to succinctly and simply explain the Automatic Semicolon Insertion (or “ASI”) foundations in this essay.
  • PS: Try manually inserting a semicolon where one is needed. Don’t rely on the qualities of language.
  • When JS detects a LineTerminator (enter, etc.) ahead, it automatically adds a semicolon after the “” and “”)” symbols, therefore the code below should function without any problems.
console.log("This is very interesting")
console.log("to learn JS")
  • Semicolons can’t be added in JS before “[]”. The code below will err.
console.log("We're trying to execute this code")[1, 3, 4].forEach(element => {
    console.log(element);
});

Before ++ and — JS will add a semicolon


++ 
y

It is parsed as x; ++y;, not as x++; y

Semicolon will be added automatically at the end of the page.

Below statements are affected (JS will add semicolon)

  • var statement
  • expression statement
  • do-while statement
  • continue statement
  • break statement
  • return statement
  • yield statement
  • throw statement
function calculate(a, b) {
    return
    a + b;
}

Output :

Submit a Comment

Your email address will not be published. Required fields are marked *

Subscribe

Select Categories