114

Possible Duplicate:
What does the leading semicolon in JavaScript libraries do?

I have noticed a lot of jQuery plugins start with

;(function(){ /* something in here */ })();

I just wondered what the beginning semi-colon was for, as well as the empty parentheses at the end.

Community
  • 1
  • 1
benpalmer
  • 2,057
  • 3
  • 17
  • 21

2 Answers2

149

The semi-colon is there in case you include this script just after some 'bad' script that doesn't properly close off its last line with a semi-colon. In this case it's possible the two scripts would be combined and result in invalid code. For example if you are merging multiple script into a single response.

The () at the end is executing the function. This is creating a closure. Private variables and methods can be declared within the scope of this function that cannot be accessed from outside the script.

James Gaunt
  • 14,631
  • 2
  • 39
  • 57
  • 6
    This has nothing to do with a "bad" script -- it has to do with writing in a semi-colon free style. –  Aug 22 '11 at 09:42
  • 103
    IMHO that is "bad" script. – James Gaunt Aug 22 '11 at 09:43
  • @pst: i didn't get that. care to elaborate? – naveen Aug 22 '11 at 09:44
  • 8
    @James Gaunt I write semicolon-free Javascript. I have yet to find a good reason *for* using semicolons (this particular case, where a line begins with a `(` is a reason to use a `;` to ensure the previous statement is terminated). By saying "bad" an unfair -- albeit popular -- judgement is being passed. If the tooling to combine multiple script files (or to minimize the code) breaks Javascript, then it is the *tooling* at fault and not a "bad" script. –  Aug 22 '11 at 10:46
  • 5
    @pst - there are lots of good reasons for always using semi-colons in this link: http://stackoverflow.com/questions/444080/do-you-recommend-using-semicolons-after-every-statement-in-javascript#444084 – James Gaunt Aug 22 '11 at 11:59
  • 1
    @James Gaunt I see none, actually. Lots of links. Lots of subjectivity. Once the rules for when to start a line with a semi-colon are known then all the "but will be interpreted as" are completely meaningless -- in my coding style the *only* time I do is then it starts with `(` as I avoid the use of `++`, `--` and it doesn't make sense to start a statement with another thing which could throw off ASI such as (such as a unary `-` or `[`). I also ignore anything dealing with broken tooling as broken tooling is broken tooling. Good clean coding is *independent* of the use of semi-colons. –  Aug 22 '11 at 18:38
  • 5
    @pst. Fair enough. I'm sure you can code fine in your style. Indeed best practice is subjective. Your statement 'Once the rules for when to start a line with a semi-colon are known...' exposes why always using a semi-colon is best practice. Because then you don't need to know these additional rules, and hope everyone else does. If we all had perfect knowledge all the time we could program in binary, but we don't. Best practice is there to assist us. It's up to you of course whether you choose to follow it or not. – James Gaunt Aug 22 '11 at 19:31
  • 3
    @James Gaunt I reject that "knowing" to add a `;` in front of a line that begins with `(` adds *any* more complexity than all the other rules that are known. In fact, I would say it is "harder" to *remember* this erroneous code *with a semi-colon*: `return \n value;` Thus the semi-colon *did nothing* to save one who does not understand the JavaScript language. Teaching beginners and/or reinforcing that a semi-colon is somehow "required" [for "good" code] at the end of every line strikes me as silly. But that's just me :) –  Aug 22 '11 at 20:18
  • 1
    This is an element of 'defensive' coding style, it prevents a certain class of bugs that stem from missing semicolons in earlier scripts. – Mike Lyons Jan 24 '19 at 23:19
10

This construct :

(function(){ /* something in here */ })()

Is used to create a new scope in Javascript.

More info on function scope here.

Regarding the semicolon, I never seen it before. I think it's a security for when you concatenate several scripts, since semicolons are optional in some cases at the end of the file.

slaphappy
  • 6,894
  • 3
  • 34
  • 59