views:

760

answers:

7

I've seen different developers include semicolons after functions in javascript and some haven't. Which is best practice?

function weLikeSemiColons(arg) {
   // bunch of code
};

or

function unnecessary(arg) {
  // bunch of code
}
A: 

Just stay consistent! They are not needed, but I personally use them because most minification techniques rely on the semi-colon (for instance, Packer).

Josh Stodola
+3  A: 

Really just depends on your preference. I like to end lines of code with semi colons because I'm used to Java, C++, C#, etc, so I use the same standards for coding in javascript.

I don't typically end function declarations in semi colons though, but that is just my preference.

The browsers will run it either way, but maybe some day they'll come up with some stricter standards governing this.

Example of code I would write:

function handleClickEvent(e)
{
     // comment
     var something = true;  // line of code
     if (something)  // code block
     {
        doSomething();  // function call
     }
}
regex
lines should definitely be terminated with semicolons, tho. otherwise a minifier might break functionality entirely
David Hedlund
@david: in that case the minifier is broken, surely?
DisgruntledGoat
+6  A: 

JS Lint is de-facto convention, and it says no semicolon after function body.

David Hedlund
I've witnessed first hand a function that failed because of a lack of semicolon. I completely disagree that leaving it off is a convention. While 99.99% of the time it won't break, there are certain situations where I've noticed IE has been unable to interpet JavaScript without the semicolon.
MillsJROSS
My response only deals with function definitions such as in the question's two examples. In these cases, a terminating semicolon is not needed in *any* browser, in *any* situation. I guess you might be thinking of function expressions. They're a completely different matter, and not one that was addressed in the original question.
David Hedlund
+21  A: 

Semicolons after function declarations are not necessary at all.

The grammar of a FunctionDeclaration is described in the specification as this:

function Identifier ( FormalParameterListopt ) { FunctionBody }

There's no semicolon grammatically required, but might wonder why?

Semicolons serve to separate statements from each other, and a FunctionDeclaration is not a statement at all.

FunctionDeclarations are evaluated before the code enters into execution, hoisting is a common word used to explain this behavior.

The terms "function declaration" and "function statement" are often wrongly used interchangeably, because there is no function statement described in the ECMAScript Specification, however there are some implementations that include a function statement in their grammar, -notably Mozilla- but again this is non-standard.

However semicolons are always recommended where you use FunctionExpressions, for example:

var myFn = function () {
  //...
};

(function () {
  //...
})();

If you ommit the semicolon after the first function in the above example, you will get completely undesired results:

var myFn = function () {
  alert("Surprise!");
} // <-- No semicolon!

(function () {
  //...
})();

The the first function will be executed immediately, because the parentheses sorrounding the second one, will be interpreted as the Arguments of a function call.

Recommended lectures:

CMS
This is not entirely true. See: http://magnetiq.com/2009/07/22/when-semicolons-are-not-optional-in-javascript/
Tomalak
Edited to clarify, that article talks about **function expressions**
CMS
Not entirely familiar with ECMA, but that is the standard I use as well. Good post. Most of the tuts I see online and code samples I DL use that standard, so I've just adapted to it.
regex
+2  A: 

Gosh, this is a popular question around here. There's at least 30 answers to it here:

Should I use semi-colons in javascript?

Do you recommend using semicolons after every statement in JavaScript?

Are semicolons needed after an object literal assignment in JavaScript?

DOK
A: 

I use them after function-as-variable declarations:

var f = function() { ... };

but not after classical-style definitions:

function f() {
    ...
}
Gabe Moothart
A: 

It's actually more than an issue of convention or consistency.

I'm fairly certain that not placing semicolons after every statement slows down the internal parser because it has to figure out where the end of the statement is. I wish I had some handy numbers for you to positively confirm that, but maybe you can google it yourself. :)

Also, when you are compressing or minifying code, a lack of semi-colons can lead to a minified version of your script that doesn't do what you wanted because all the white space goes away.

Mason
The question was geared towards if semicolons had to be after functions, not every statement. I agree that you should place semicolons after every statement, and I've seen other stackoverflow consensus saying the same.
macca1
Agreed, and failing to put semicolons after functions will result in that minification issue I mentioned. Good luck sir.
Mason