Discussion on the use of the semicolon of javascript

  • 2020-06-12 08:27:50
  • OfStack

Is it necessary to put a semicolon at the beginning of function? Should js statements be followed by semicolons? Should javascript braces be followed by semicolons? What are the exclamation marks and semicolons at the beginning of function?

After multiple Js files are integrated into one file, syntax errors can be avoided when compressing the code, which can be handled as follows

1. Put a semicolon before js

For example:; (function ($) {... The code here... }) ();
The semicolon in Javascript indicates the end of a statement, and it is added at the beginning, in order to separate 1 from other methods during compression, indicating the beginning of a new statement

2. js function followed by a semicolon

For example,


//  The module 1
//  There is some code in front of it 
var Manager = {
 prop: '',
 method: function () {

 }
}
//  The module 2 , starts with a function that executes immediately 
(function () {
 //  code 
})()

After compression, it becomes:}}(function, it will be executed as a function, so the whole parse will be wrong


var Manager = {prop: '',method: function (){}}(function () {})()

Solution: Put a semicolon after the Manager function

This is the end of this article, I hope you enjoy it.


Related articles: