25

Possible Duplicate:
JavaScript: When should I use a semicolon after curly braces?

Someone added semicolon after function declaration, but someone not. Is this a good practice to add semicolon after function declaration?

function test(o) {
}

function test(o) {
};
Community
  • 1
  • 1
Adam Lee
  • 24,710
  • 51
  • 156
  • 236
  • 3
    This might be relevant: [should a multiline var f = function() { /* Code */ } get a semicolon](http://stackoverflow.com/q/4515084/990877). To summarize: close with a `;` when declaring the function in an assignment expression (i.e. `var f = function() {}`). – Peter-Paul van Gemerden Aug 15 '12 at 23:27

6 Answers6

42

A function declaration does not need (and should not have) a semicolon following it:

function test(o) {
}

However, if you write a function as an expression, like the variable initializer below, then the statement should be terminated with a semicolon, just like any other statement would be:

var a = function test(o) {
};

See more about constructor vs declaration(statement) vs expression.

Marius Mucenicu
  • 1,685
  • 2
  • 16
  • 25
Lee
  • 13,462
  • 1
  • 32
  • 45
  • 1
    Why does a function declaration not need a semicolon? It seems weird that variable declarations and variable calls use semicolons but only function calls and not function declarations do. – Aslan French Jul 03 '19 at 04:53
5

What's actually happening there is you're adding an empty statement after the function.

function test (o) { return o; };

could be seen as being similar to:

var test = 0;;

That second semicolon isn't an error per-se. The browser treats it like a statement where absolutely nothing happened.

There are two things to keep in mind, here.

This applies ONLY to function-declarations and control-blocks (for/if/while/switch/etc).

Function-declarations should be defined at the bottom of your scope, so you don't run into problems like this:

function test () {}
(function (window, document, undefined) { /* do stuff */ }(window, document));

Because the browser will assume that you mean function test() {}(/*return value of closure*/); Which is an error. A very bad and nasty error which is very easy to overlook.

But that's okay, because function-declarations can go under return statements and still work just fine.

So even if you wanted to go:

function doStuff () {
    return (function () { /*process stuff*/ test(); }());
    function test () {}
}

That's going to work just peachy.

Norguard
  • 26,167
  • 5
  • 41
  • 49
4

Semicolons and function declarations:

function test(o) {
    // body
} // semicolon no

var test = function (o) {
    // body
}; // semicolon yes

See JSLint for formatting code questions.

artlung
  • 33,305
  • 16
  • 69
  • 121
  • 1
    Semicolon is not required in the second type of function declaration as well. The code after it runs without any problems. – gegobyte Jan 27 '17 at 17:27
3

No.

You don't need semicolons when defining a function like that.

However, if you define a function like this:

var test = function (o) {
}

It's not strictly necessary, but you may want to use them, especially if you put the function on one line.

The first way defines a function, but the second way assigns a function to a variable, and thus is a statement. Most statements are semicolon delimited. Defining functions could be considered a common counterexample, as not many people do use them.

Kendall Frey
  • 43,130
  • 20
  • 110
  • 148
1

To the browser, it doesn't matter. For matter of semantics, it only matters if you're prototyping a function or using the function statement.

function stuff(stuff) {
     alert(stuff);
} //don't need a semicolon

Object.prototype.stuff = function(stuff) {
    alert(stuff);
}; //need a semicolon
var stuff = function(stuff) {
    alert(stuff);
}; //need a semicolon
Polyov
  • 2,281
  • 2
  • 26
  • 36
0

Semicolon is not required while defining a function, but putting it on is not a mistake either.

One exception though, if you use function wrappers and pass the parameters, you need to add semicolons in between, example:

(function(v){alert(v)})('1');
(function(s){alert(s)})('0')

... Otherwise forget about them ...

Anonymous
  • 4,692
  • 8
  • 61
  • 91