9

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

I am reading the jquery flexslider source code and i see a ; before the document ready call

;(function ($) {...

Can anyone tell me why we need a ; ?

Community
  • 1
  • 1
Pratik Khadloya
  • 12,509
  • 11
  • 81
  • 106

3 Answers3

5

This is just to protect against any previous code lines that might be missing a semicolon.

If you know that the code before has a semicolon at the end, this semicolon is not needed at all.

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
3

It's included in case...

  • the code gets grouped into the same file with other code, and

  • the other code didn't include a semicolon at the end.


For example...

    (function() {

       // some bundled plugin

    })()  // <--- no semicolon

//  v--- semicolon saved the day    
    ;(function ($) {

       // flexslider plugin

    })();

Without the semicolon, the () around the flexslider plugin would have been interpreted as a function call, and would have tried to call the return result of the previous function.

1

This is not a typo. ; prevents javascript errors in compliled/minified/compressed files. For example when several independent libraries/plugins get compressed together.

dfsq
  • 191,768
  • 25
  • 236
  • 258