What does the semicolon at the beginning of the line mean?

11

I was analyzing a code and found the following excerpt:

;(function ($, window, document, undefined) {
    //...
})(jQuery, window, document);

Notice that at the beginning of the first line, the ";" character exists; (semicolon).
What is the function of the semicolon at the beginning of the line?

    
asked by anonymous 02.06.2014 / 13:39

1 answer

9

The function of the semicolon at the beginning of the line is to successfully concatenate a new code when the previous one does not include the ";" in the end.

var n = a + b 
(c + d).print() 

Without the comma, the second line would have been interpreted as a function call. Home The system would interpret as follows:

var n = a + b(c + d).print();

Note that the variable "b" would be interpreted as a function, returning the following error:

ReferenceError: b is not defined

In conclusion, it is good practice to always terminate a statement with the ";", but when a line starts with parentheses it is also good practice to precede it with ";", especially when we need to change third party code. p>

;(c + d).print()
    
02.06.2014 / 13:39