This question already has answers here:
Semicolon before self-invoking function? [duplicate]
(4 answers)
Closed 9 years ago.
I was just going through the source code of cordova.js, its structure is like
;(function() { cordova code I have yet to understand })();
Just curious what does the first semi-colon imply?
Is it just to make sure that there is a semicolon preceding the anonymous function or does it mean something else?
It is a defensive semicolon, this is in case someone concatenates some JavaScript before your code, and this concatenated code forgot to put a terminating semicolon.
It's just to prevent an error if you combine multiple js files. So you can remove it if you want.
Related
This question already has answers here:
Location of parenthesis for auto-executing anonymous JavaScript functions?
(4 answers)
Closed 7 months ago.
I've inherited a project with some Java Script. I know nothing about js. There are some auto-build steps involved, the result of which is an anonymous function. The version is version control is different from the version I build, even though the environments were supposed to be the same.
One version is, void parameter list inside the evaluation brackets:
(function(){...}( ))
The other version is, void parameter list outside the evaluation brackets:
(function(){...} ) ( )
Are these two forms technically the same? Is one form technically an error? Is either form actually an error? Or what?
In this case, both are equivalent and valid.
Note that for the first option, when the outer parentheses are excluded, that will result in a SyntaxError
function(){...}() // error
(function(){...}()) // no error
(function(){...})() // no error
This question already has answers here:
What does the exclamation mark do before the function?
(8 answers)
Closed 5 years ago.
What does this mean?
!function a(){
}({
x: [function(){alert(2);}]
})
When I reading some codes in a chrome extension, I have found this block of code. I am confused how this should work.
It's just another way of writing an IIFE, using a ! operator instead of parentheses. There are a variety of operators that can be used this way:
! function(text) {
console.log(text);
}('hello');
This question already has answers here:
Why does the value returned should be on the same line as the return statement in JavaScript?
(4 answers)
Closed 6 years ago.
This has been the source of my pain for many hours. Can anyone explain why this is the case?
function x(){
return //when there's a line break it doesn't work
2;
};
alert(x());
function y(){
return 4; //when there's no line break it works
};
alert(y());
//Can anyone explain this?
I always thought that JavaScript didn't care about line breaks. If you have links to ECMA official documentation on this, I'd be grateful. Thanks!
Here are the ECMAScript rules for Automatic Semicolon Insertion. The relevant section is:
When a continue, break, return, or throw token is encountered and a LineTerminator is encountered before the next token, a semicolon is automatically inserted after the continue, break, return, or throw token.
In short, your code is parsed as if it were:
return;
2;
Unlike most other languages, JavaScript tries to "fix" your code by adding semicolons where it thinks you have forgotten them.
Return statements are such a case - if there is a linebreak after a return statement, it will be interpreted as return;
JavaScript treats a endline after return as if there was an ;
So in:
return
2;
2; is unreachable code.
And a sole return in JavaScript returns undefined. Like a function without a return does.
JS gramma from mozilla
http://www-archive.mozilla.org/js/language/grammar14.html#N-Statement
Note:
the OptionalSemicolon grammar state can sometimes reduce to «empty»
This question already has answers here:
What does the leading semicolon in JavaScript libraries do?
(6 answers)
What's the purpose of starting semi colon at beginning of JavaScript? [duplicate]
(2 answers)
Closed 9 years ago.
I've seen some strange ; at the beginning of a function in some jQuery plugins source code like this:
;(function ($) {.....
Can someone explain why they need to use ; in this case?
This semicolon will help you to properly concatenate a new code into a file when the current existed code in this file does not include a ; at the end.
For example:
(function() {
})() // <--- No semicolon
// Added semicolon to prevent unexpected laziness result from previous code
;(function ($) {
})();
Without the semicolon, the second () would have been interpreted as a function call, and will tried to call the return result of the first function
This is just to make sure to terminate any previous instruction.
the semi colon before function invocation is a safety net against
concatenated scripts and/or other plugins which may not be closed
properly.
https://github.com/shichuan/javascript-patterns/blob/master/jquery-plugin-patterns/extend.html
This question already has answers here:
What does the exclamation mark do before the function?
(8 answers)
Closed 9 years ago.
I read this on the javascript garden site. Can someone explain how it works?
!function(){console.log("hi")}()
The 'executing' parens at the end can't be done legally after a function expression. A typical (more sensical, IMO) way of writing this is with more parentheses:
(function(){console.log('hi')})()
By prepending the ! before the function expression, the JS interpreter reads the function and then runs it. This is because of the precedence of the ! operator vs. calling a function with the final ()
Look at this answer
tl;dr it defines a function to print out 'hi' and immediately calls it.