I'm looking at an open source project, and see something like this :
;(function(){
// codes here
})()
I'd like to know whether the semicolon there has a special meaning?
This is because the ASI (Automatic Semicolon Insertion) allows you to avoid semicolons.
For example, you can write this kind of code with no error:
var a = 1
a.fn = function() {
console.log(a)
}
See? Not a single semicolon.
However, there are cases where the semicolon isn't inserted. Basically, in real projects, there is one case where it isn't: when the next line starts with a parenthesis.
The javascript parser will take the next line as an argument and not automatically add a semicolon.
Example:
var a = 1
(function() {})()
// The javascript parser will interpret this as "var a = 1(function() {})()", leading to a syntax error
To avoid this, there are several ways:
Use the following structure:
!function() {}()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With