Always use semicolons in JavaScript

Posted on in JavaScript

According to Javascript; the semicolon after a statement (for example a function), is optional.
However, it's much better to end your line with a semicolon. A validator like JSLint for example, will throw a warning, even though
the browser won't and accepts it.

See below, which strange things can happen when you won't end your line with a semicolon.
Javascript errors because of the order of javascript functions mixed with jQuery plugins...

foo is a simple javascript function.
Baz is a simple self running jQuery plugin. (because of the parentheses surrounding it).

foo = function(){
alert("function 1"); 

$.fn.Baz = function(opt){
alert("function 2");

See below the script, without strange javascript errors.

The answer is really logical.
Because there is no semicolon, the jQuery plugin (Baz) will directly execute, because of the parentheses.
Like it's a parameter of the javascript function you wrote before. (foo), and then throws errors.


When you mix your jQuery plugins with native javascript classes and functions in different orders; these kind of errors can happen.
This scenario above shows you why you should always end your statements with a semicolon.

Post a Comment

Your email is never published nor shared.
Required fields are marked *