174

In several JavaScript libraries I saw this notation at the very beginning:

/**
 * Library XYZ
 */
;(function () {
  // ... and so on

While I'm perfectly comfortable with the "immediately executed function" syntax

(function(){...})()

I was wondering what the leading semicolon is for. All I could come up with is that it is an insurance. That is, if the library is embedded in other, buggy code, it serves as an "the last statement ends here at the latest" kind of speed bump.

Has it got any other functionality?

1

6 Answers 6

146

It allows you to safely concatenate several JavaScript files into one, to serve it quicker as one HTTP request.

9
  • 16
    But it would not be necessary, if all files would be coded up correctly, would it?
    – Boldewyn
    Dec 9, 2009 at 13:46
  • 8
    No: In your example, you'd get (function(){...})()(function(){...})(). Dec 9, 2009 at 13:52
  • 8
    That I meant with 'coded up correctly', that every library ends with the correct amount of trailing semicolons...
    – Boldewyn
    Dec 9, 2009 at 14:05
  • 6
    Yeah Boldewyn, but that’s simply not the case. Dec 23, 2009 at 10:46
  • 14
    Two badly coded files will not be fit just by the fact that a third file contains this dirty fix. Why can not the concatenator add extra semicolons between files in the result? Dec 3, 2015 at 12:06
34

The best answer was actually given in the question, so I will just write that down here for clarity:

The leading ; in front of immediately-invoked function expressions is there to prevent errors when appending the file during concatenation to a file containing an expression not properly terminated with a ;.

Best practice is to terminate your expressions with semicolons, but also use the leading semicolon as a safeguard.

5
  • Why also terminate your expressions with semicolons, if you're using defensive semicolons? Either you take the chance of relying on semicolons being there at the end of every line, or you use defensive semicolons. Doing both makes people incorrectly conclude that there is a reason to do both. The advice to use defensive semicolons is good; the advice to also replace every instance of "\n" with ";\n" makes no sense.
    – Val Kornea
    Apr 1, 2015 at 12:29
  • 5
    @VladimirKornea: because you're not always using only your own libraries :) Feb 15, 2016 at 22:02
  • 6
    Because you can't rely on someone else's code following your conventions. Code defensively and then you don't have to. Feb 16, 2016 at 22:40
  • 1
    @VladimirKornea You can think of that as defensive as well, if you want -- it defends against someone else's code that doesn't always begin with defensive semicolons. Mar 20, 2017 at 1:08
  • re: "best practice", that's subjective as ASI is an intentional feature of the language. I've seen increasing numbers of people eschew semicolons other than just before IFFEs (or other statements beginning with (, [, etc.). There's even Prettier/eslint fix settings for this style. For more info, see blog.izs.me/2010/12/… Jun 16, 2022 at 20:34
32

In general, if a statement begins with (, [, /, +, or -, there is a chance that it could be interpreted as a continuation of the statement before. Statements beginning with /, +, and - are quite rare in practice, but statements beginning with ( and [ are not uncommon at all, at least in some styles of JavaScript programming. Some programmers like to put a defensive semicolon at the beginning of any such statement so that it will continue to work correctly even if the statement before it is modified and a previously terminating semicolon removed:

var x = 0 // Semicolon omitted here
;[x,x+1,x+2].forEach(console.log) // Defensive ; keeps this statement separate

Source:

JavaScript: The Definitive Guide, 6th edition

9

This is referred to as a leading semicolon.

Its main purpose is to protect itself from preceding code that was improperly closed, which can cause problems. A semicolon will prevent this from happening. If the preceding code was improperly closed then our semicolon will correct this. If it was properly closed then our semicolon will be harmless and there will be no side effects.

1
  • I would maybe not say "improperly closed". Avoiding semicolons except leading ones where necessary is actually a fairly popular coding style. Making use of ASI isn't improper (though it can definitely be improperly used or misunderstood, leading to bugs). Jun 16, 2022 at 20:38
8

A one-line answer is to safely concatenate multiple JavaScript files. Using a semicolon does not raise an issue.

Suppose you have multiple functions:

IIFE 1

(function(){
  // The rest of the code
})(); // Note it is an IIFE

IIFE 2

(function(){
   // The rest of the code
})(); // Note it is also an IIFE

On concatenation it may look like:

(function(){})()(function(){})()

But if you add a semicolon before the function it will look like:

;(function(){})();(function(){})()

So by adding a ;, it takes care if any expression is not properly terminated.

Example 2

Assume you have a JavaScript file with a variable:

var someVar = "myVar"

Another JavaScript file with some function:

(function(){})()

Now on concatenation it will look like

var someVar = "myVar"(function(){})() // It may give rise to an error

With a semi-colon, it will look like:

var someVar = "myVar";(function(){})()
1

It's good when you minify JavaScript code. It prevents unexpected syntax errors.

3
  • Like what? Has the semicolon any significance for the following code or is it just for hypothetical buggy code merged in front of the actual library?
    – Boldewyn
    Dec 9, 2009 at 13:50
  • In, that codes alone, no special meaning, but when that code is is in the middle of others codes and when you minify it to single line, there can be unexpected errors, like (1) semicolon is missing in previous lines, (1) previous one is also functions so it will be ()()()(), when get error, hard to debug, we cant say it buggy, because before minify its running fine.
    – YOU
    Dec 9, 2009 at 14:18
  • 3
    But surely it's the responsibility of the minifier to handle this correctly. Are buggy minifiers the norm nowadays?
    – Val Kornea
    Apr 1, 2015 at 12:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.