2

I have a file jquery.flex.js which starts with

enter image description here

What does beginning mean?

Michal B.
  • 5,676
  • 6
  • 42
  • 70
Dims
  • 47,675
  • 117
  • 331
  • 600

3 Answers3

5

The purpose of this semicolon is to avoid an error if the file is concatenated with another one.

For example suppose you have two files like this :

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

It's OK as long as it's in two separate files but it's a error if you concatenate them as is frequently done to reduce the number of requests. What happens is that basically you have

(A)()(B)()

and that the engine is trying to call the result of (A)() with argument B.

Adding a semicolon to delimit the statements fix the problem.

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
  • I'm not sure if the OP knows the names for punctuation, but it does say 'comma' in the title :-/ – David Thomas Apr 14 '14 at 09:18
  • 2
    @DavidThomas: I changed the title. The op most probably didn't know the proper name for the semicolon. But the later question "What does beginning mean?" says it all. – Michal B. Apr 14 '14 at 09:20
  • Yes, you did; but I'm not entirely convinced that the OP used the wrong word, and by changing the title you -potentially- completely changed the question. But you could well be right. – David Thomas Apr 14 '14 at 09:21
  • Huh, interesting use case. We concat all our javascript files and I've never run into this problem. I guess all our files have a trailing newline though. The file posted here has one at the top of the file as well, which should work just as well as a semicolon. – Abhi Beckert Apr 14 '14 at 10:47
  • You rarely fall on this one but it happens, especially when you use a lot of IIFE. It happened to me. – Denys Séguret Apr 14 '14 at 11:00
1

Since most modules start with a self-executing function (function() {/* module code */}()) the trailing semicolon is just a security issue:

Take this example: if you load two js modules on your website, and the first one ends without a semicolon:

(function firstModule (window, undefined) {
    /* whatever this module does comes here */
})()

and the second one looks the same:

(function secondModule (window, undefined) {
    /* whatever this second module does comes here */
})()

Then you got a problem. Because your first module will execute itself and the second module will try to execute the result of the first modules self-execution:

(firstModule)()(secondModule)()

This would mostly break your code, or at least lead to unexpected behaviour.

So it is a simple pattern to add a leading semicolon to your module code:

;(firstModule)();(secondModule)()

And everything is fine.

jukempff
  • 965
  • 6
  • 12
-1

I don't know what you're asking for, but your title says:

What does comma mean in javascript?

It is used as a separator.

Amit Joki
  • 58,320
  • 7
  • 77
  • 95
  • The title was changed; You might delete this answer. You also have enough rep to update the title and flag for duplicates so… – dlamblin Aug 21 '18 at 06:34