When should I use a semicolon after curly braces?
It matters too when you intend to minify your code.
So I personally add one after every }
where automatic semicolon insertion (ASI) would insert one.
I wrote a post about ASI in JavaScript.
You use a semicolon after a statement. This is a statement:
var foo = function() {
alert("bar");
};
because it is a variable assignment (i.e. creating and assigning an anonymous function to a variable).
The two things that spring to mind that aren't statements are function declarations:
function foo() {
alert("bar");
}
and blocks:
{
alert("foo");
}
Note: that same block construct without semi-colon also applies to for
, do
and while
loops.
Don't use a semicolon:
...if it's just your every-day function declaration:
function foo() {
} // No semicolon
Use a semicolon:
...if it's an assignment:
var foo = function() {
}; // Semicolon
...or a self invoking function:
(function () {
})(); // Semicolon