Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is semicolon at the beginning of code still good practice?

I have been taught that it is a good practice to always insert a semicolon at the beginning of JavaScript code, as following:

;(function(){

})();

However, many popular JavaScript libraries/frameworks do not use this, such as jQuery, Backbone, etc.

I believe that the semicolon at the beginning is supposed to prevent bad code to break the minified/compressed code, etc. But still, why no one is using it anymore?

Has the semicolon at the beginning turned out to be useless for some reason?

like image 399
Frank Avatar asked Feb 22 '13 14:02

Frank


People also ask

Do you need semicolons in JavaScript 2022?

To recap, semicolons are not mandatory in JavaScript. Instead, the Automatic Semicolon Insertion (ASI) process adds semicolons where necessary. However the ASI is not correct 100% of the time. Also, in some situations, you simply have to use a semicolon.

Is it good practice to use semicolons in JavaScript?

Semicolons are an essential part of JavaScript code. They are read and used by the compiler to distinguish between separate statements so that statements do not leak into other parts of the code. The good news is that JavaScript includes an automatic semicolon feature.

Should I use semicolons in ES6?

Even Google's summarized ES6 style guide continues to require semi-colons. There is a good reason. As developers we should know that when you remove a feature there will inevitably be users who relied on that feature to help them accomplish their task (in this case, we all program and write code :)).

Why do you need semicolons in code?

The Semicolon lets the compiler know that it's reached the end of a command. Semicolon is often used to delimit one bit of C++ source code, indicating it's intentionally separated from the respective code.


2 Answers

The more common practise is to add a semicolon at the end of the file. The issue is, when you concatenate two files like this:

// file1.js
(function() {
})()

// file2.js
(function() {
})()

Without putting a semicolon at the end of file1, it will try to invoke the return value from the function in file1 with the function in file2. Putting a semicolon at the end of each file will solve this (as will putting them at the beginning).

Another way is to turn the function invocation into a statement like this:

!function() {
}();

I think this is also recommended by JSLint, because in this case you don't have to worry about semicolons (although you should use them anyway but that's a different discussion altogether ;).

like image 163
Daff Avatar answered Oct 21 '22 07:10

Daff


Ideally minifying and merging multiple files should have no affect your coding style. You should be able to write your program as you wish and then use an automated tool to correctly merge and minify your project.

There are lots of automated tools that do this. Take a look at UglifyJS 2 for example. I'm sure you'll be able to find many more such tools if you look around.

Getting back to the question, it's important to insert a semicolon after an immediately invoked function expression as Daff pointed out. However there's no reason to put a semicolon before it. If you be a good boy and put a semicolon after every statement and expression then you should never have any problems.

Do not let JavaScript ever do automatic semicolon insertion for you.

The only place where it's permissible to not put a semicolon is after a function declaration:

function foo() {} // it's alright to not put a semicolon here

However if you're using a function expression then always put a semicolon.

(function foo() {})(); // you should put a semicolon here

Putting semicolons anywhere else is just confusing. Especially at the beginning of a line. People from other programming backgrounds may also think it's the start of an end of line comment.

like image 35
Aadit M Shah Avatar answered Oct 21 '22 08:10

Aadit M Shah