CrazyMatt CrazyMatt - 3 months ago 9
Javascript Question

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

I sometimes like to work quickly, so I set a macro in my editor to always add a semicolon after a 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?

Answer

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.