Semicolons in JavaScript: To Use or Not to Use?

Semicolons in JavaScript: To Use or Not to Use?

During one of our first JavaScript lectures at the Flatiron School, the instructor mentioned that semicolons are optional in JavaScript… except when they’re not 🤔

I decided to look more into semicolon usage in JavaScript to truly understand why we would or would not want to use them, and use this knowledge to avoid creating any bad habits early on.

Peter Arkle semicolon drawing

Peter Arkle, https://opinionator.blogs.nytimes.com/2012/07/02/semicolons-a-love-story/

Automatic Semicolon Insertion (ASI)

The reason semicolons are sometimes optional in JavaScript is because of automatic semicolon insertion, or ASI. ASI doesn’t mean that actual semicolons are inserted into your code, it’s more of a set of rules used by JavaScript that will determine whether or not a semicolon will be interpreted in certain spots. I found a helpful lecture from Fullstack Academy on the topic, which you can check out here. I also found a blog post from Bradley Braithwaite on the topic. Below I highlight the main takeaways from these resources.

3 Automatic Semicolon Insertion Rules:

Here are 3 main points to be aware of when it comes to ASI.

1- A semicolon will be inserted when it comes across a line terminator or a ‘}’ that is not grammatically correct. So, if parsing a new line of code right after the previous line of code still results in valid JavaScript, ASI will not be triggered.

Example 1:

var beef 
var cheese 

will become:

var beef; 
var cheese; 

Example 2:

var a 
    b= 
    3;

here, grammar doesn’t expect to see b after a, so ASI is triggered

var a; 
b = 3;

Example 3:

a = b + c 
(d + e).print() 

equals:

a = b + c(d + e).print();

2- If the program gets to the end of the input and there were no errors, but it’s not a complete program, a semicolon will be added to the end. Which basically means a semicolon will be added at the end of the file if it’s missing one.

3- There are certain places in the grammar where, if a line break appears, it terminates the statement unconditionally and it will add a semicolon. One example of this is return statements.


function getCheese() {
    return 
    { 
       cheeseType: "Gouda"
    } 
}

This would trigger ASI and result in:


function getCheese() {
    return; 
    { 
       cheeseType: "Gouda"
    } 
  }

Expressions in a return statement should begin on same line, like this:


function getCheese() {
    return { 
       cheeseType: "Gouda"
    } 
  }

When Should I Not Use Semicolons?

Here are a few cases where you don’t need semicolons:

if (…) {…} else {…}
for (…) {…}
while (…) {…}

Note: You do need one after: do{…} while (…);

Final Thoughts

If you’re going to write your JavaScript without optional semicolons, it’s probably good to at least know what ASI is doing. For example, compression or minification could cause your valid code to throw an error because those programs may rely on semicolons.

Also, it can be harder to debug without semicolons since your code may be concatenating together without you realizing it. If you put a line break where there shouldn’t be one, ASI may jump in and assume a semicolon even if there shouldn’t be one.

keep calm it's only a semi colon

https://www.everywordcounts.co.uk/no-need-to-be-scared-of-semicolons/

Companies and open source projects will likely have a preference one way or another, so be sure to note what it is.

Finally, if you think you may be running into errors due to ASI, check out Babeljs.io to debug – it allows you to put in your code and shows you where ASI semicolons are being inserted.

Source: dev