1

In the lodash library line, why is there a defensive semicolon on the first line?

;(function(window) {
    ...
}(this));

I recently read in Definitive JavaScript about defensive semicolons being used to protect against users who don't use semicolons properly, but as there is no preceding code, I don't see the point. Is this in case the library is concatenated on to the end of another library?

FishBasketGordo
  • 22,904
  • 4
  • 58
  • 91

2 Answers2

6

In case you use a javascript compressor/minifier, and the previous plugin does not have a ; at the end, you might run into troubles. So, as a precaution, ; is added.

Also, It safely allows you to append several javascript files, to serve it in a single HTTP request.

karthikr
  • 97,368
  • 26
  • 197
  • 188
0

That semicolon is also used to ensure that it is not interpreted as a continuation of the previous statement:

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

More detail: https://stackoverflow.com/a/20854706/1048668

Community
  • 1
  • 1
voltrevo
  • 9,870
  • 3
  • 28
  • 33