简体   繁体   中英

Is always putting a semicolon after an end curly-brace a bad idea?

I sometimes like to work quickly, so I set a macro in my editor to always add a semicolon after an end curly-brace, even when not necessarily needed. For example:

var test=function(){
    //I realize the semicolon after the curly-brace below is required
};

function test(){
    //But is it safe to have on where it isn't required, like below?
};

As in, will it create errors, slow down the site, or create compatibility issues? I will remove these additional semicolons when I am finished developing the product, but for now, is it OK?

Note that there are cases where adding a semicolon after an ending curly bracket can cause issues, eg:

let data = {
  item1: [],
  item2: {}; // This semicolon is a syntax error
};

On a program level, it makes no difference to have this:

function() {

};

Or this:

function() {

}

I do recommend that you pick one way to do it and stick with it for the purpose of consistency.

You should also read through this post: Do you recommend using semicolons after every statement in JavaScript? A lot of good points on this subject are made there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM