1

Is it ever necessary to add a semicolon at the end of a function definition like the following

function helloWorld() { //some code }; //note the semicolon here at the end is it ever necessary? Ever 

I have a friend that swears this is optional. I think it is never optional I have never read anything about it being optional at the end of a function. I have never seen any one ever do that "Except him". Is the semi colon optional.

var hellWorld = function (){ }; // the semicolon here it is optional because you are defining a variable this is OK and optional 
6
  • And your friend would be right. It is optional. Especially after a function statement. Commented Mar 1, 2013 at 18:20
  • 3
    Not only is it optional, but it's also AFAIK "not recommended" (most semicolons in JS are optional, but it's "recommended" to use them anyway). Commented Mar 1, 2013 at 18:20
  • The semicolon is optional practically everywhere in JS, in that it works without semicolons (though it may not be a good idea, depending on who you ask). Commented Mar 1, 2013 at 18:20
  • 1
    Not recommended for function declarations unless you're storing it in a variable. Commented Mar 1, 2013 at 18:22
  • 2
    It's never necessary, and it's never supposed to be done either. Commented Mar 1, 2013 at 18:34

4 Answers 4

5

Semicolons delimit expressions. You can add any amount of semicolons after one, since they'd be delimiting empty statements.

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

That semicolon after the function is not "optional", it's plain redundant.

As for the actually optional ones, they are so because Javascript will add semicolons before newlines in most situations one is needed. These you should write explicitly. You'll be bitten by the lack of an auto-semicolon sooner or later.

Sign up to request clarification or add additional context in comments.

2 Comments

Upvoted. Can you help me by specifying a bit further. In a constructor function, I see the semicolon omitted. function C(){} but then in many other cases there are semicolons. The only explanation I found was on W3Schools. "Semicolons are used to separate executable JavaScript statements. Since a function declaration is not an executable statement, it is not common to end it with a semicolon." So I'm trying to get a better answer. It seems like you know what you're talking about.
The best way of learning correct syntax in the face of the optional is to use a linter. Try JSLint or JSHint: those programs will scan your code and make improvement suggestions. After using them for a while, you'll pick up the good practices.
3

In that case, yes, it's not needed.

This works fine:

function foo() {} var x = "hi"; 

This, however, is a syntax error:

var foo = function() {} var x = "hi"; 

It would be fine with a line-break after }, or with a semi-colon. You don't need both -- generally a semicolon is inserted before a line break or closing brace if the program is invalid without it. But in some cases the program still is valid without it but means something else. So you have to be careful relying on semi-colon insertion.

Example:

var x = "foo" /foo/.test(x) 

Here I wanted semi-colon insertion at the end of the first line, but got burned because the / in my RegEx could also be a division operator. So no semi-colon insertion. The fact that this is invalid when you get to the . doesn't save you, var x = "foo" / is a valid program fragment, so no semi-colon insertion.

This is why some people say "Just never rely on semi-colon insertion".

But you still don't need a semi-colon after a function definition.

1 Comment

Also, even if you don't rely on semi-colon insertion you can still be bitten by cases where a line break wasn't allowed, but you thought it was, so you get a semi-colon insertion you weren't expecting. For instance, right after the return keyword.
0

Not required.It is implicitely inserted but relying on implicit insertion can cause subtle, hard to debug problems. Don't do it. You're better than that.

There are a couple places where missing semicolons are particularly dangerous:

// 1. MyClass.prototype.myMethod = function() { return 42; } // No semicolon here. (function() { // Some initialization code wrapped in a function to create a scope for locals. })(); var x = { 'i': 1, 'j': 2 } // No semicolon here. // 2. Trying to do one thing on Internet Explorer and another on Firefox. // I know you'd never write code like this, but throw me a bone. [normalVersion, ffVersion][isIE](); var THINGS_TO_EAT = [apples, oysters, sprayOnCheese] // No semicolon here. // 3. conditional execution a la bash -1 == resultOfOperation() || die(); 

Comments

-1

Not necessary, but good style. But when using a minifier which removes extraneous white space, shortens variables etcetera, left out semicolons often give problems.

3 Comments

The first example is not good style. And if one can have a basic understanding of ASI, they can safely leave semi-colons out of their code in most places.
...and a minifier that can't deal with semi-colons properly is a minifier not worth using.
I encountered at least one problem where a JavaScript minifier removed newlines, resulting in a missing semicolon. But you are right.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.