javascript - Is semicolon at the beginning of code still good practice? - Stack Overflow

I have been taught that it is a good practice to always insert a semicolon at the beginning of JavaScri

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/pressed 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?

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/pressed 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?

Share Improve this question edited Mar 24, 2019 at 20:27 double-beep 5,53619 gold badges40 silver badges49 bronze badges asked Feb 22, 2013 at 14:48 FrankFrank 935 bronze badges 10
  • 5 No one is doing it anymore: What kind of evidence do you have to support that claim? – Andrew Whitaker Commented Feb 22, 2013 at 14:50
  • 2 If you want to talk about "good practice", I'd say move the invocation parenthesis inside the parenthesis that contain the function. – Paul S. Commented Feb 22, 2013 at 14:51
  • 1 "the semi-colon before function invocation is a safety net against concatenated scripts and/or other plugins which may not be closed properly." (jqueryboilerplate.) It is only ever necessary, if you script on a page where other 3rd party scripts are also loaded/executed. – marekful Commented Feb 22, 2013 at 14:52
  • 3 Another thing you might consider is to add a semicolon at the end of your code. It's syntactically correct, and it adds protection for the script appended to yours. – Pointy Commented Feb 22, 2013 at 14:52
  • 3 @mcpDESIGNS no, it's OK: (function(){}()) is the same as (function(){})() – Pointy Commented Feb 22, 2013 at 14:53
 |  Show 5 more ments

2 Answers 2

Reset to default 8

The more mon 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 remended 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 ;).

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 ment.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744214645a4563495.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信