;(function() {})(); in JavaScript [duplicate] - javascript

This question already has answers here:
What is the (function() { } )() construct in JavaScript?
(28 answers)
Can someone explain this JavaScript auto executing function?
(6 answers)
Closed 6 years ago.
I am looking at a JavaScript file, and it is formatted as follows:
;(function() {
// functions and stuff
})();
What does this mean, and what is this 'technique' called?

Immediately-invoked function expression

Its called Immediately-Invoked Function Expressions (IIFEs) like
// variant 1
(function () {
alert('Woohoo!');
})();
// variant 2
(function () {
alert('Woohoo!');
}());
You can read more about it here

This is an anonymous self-executing function.
I would refer you to these resources as of why:
https://en.wikibooks.org/wiki/JavaScript/Anonymous_functions
http://esbueno.noahstokes.com/post/77292606977/self-executing-anonymous-functions-or-how-to-write

Related

parentheses before function syntax js meaning [duplicate]

This question already has answers here:
What is the (function() { } )() construct in JavaScript?
(28 answers)
Closed 2 years ago.
I came across with a piece of code with new syntax for me, What this syntax means in js? I mean this parentheses at the beginning:
(function () {
//...
})()
It's called IIFE
basically, you can define a a function and invoke it immediately without declaring a name for it.

Enclosing parentheses in JavaScript [duplicate]

This question already has answers here:
What is the (function() { } )() construct in JavaScript?
(28 answers)
Closed 5 years ago.
Could I get an explanation of the follow code snippet?
(function()
{
alert();
})();
This looks like an anonymous function and the alert() function gets executed. I don't understand the semantic meaning of the outer parentheses. What does this part of the snippet mean?
(
)()
this represents the immediately executable function. in easier way, it means that function being declared and called/executed simultaneously.

Is there a way to declare and instantly run function? [duplicate]

This question already has answers here:
What is the (function() { } )() construct in JavaScript?
(28 answers)
Closed 6 years ago.
I've a code:
var myFn = function(){
//some code
}
myFn();
So I have to define a function, then run it in two rows.
Is there a way to define a function (with storage it in variable) and run it instantly, in one expression? Just a short way of this.
var myFn = function(){
console.log("test");
}()
myFn will be undefined because the function doesn't return anything

Javascript ( function () { ... } ) (); [duplicate]

This question already has answers here:
What is the (function() { } )() construct in JavaScript?
(28 answers)
How do JavaScript closures work?
(86 answers)
Closed 7 years ago.
Hello I've got a function like this one.
function doSomething () {
( function () {
// callbacks and code
}
)();
}
I am not use to that typing, I'm looking forward to understand how is interpreted by the browser or what does it mean the ( function () ) ();
Thought I know the differences between calls and assigns , I can't read this properly , surely is something simple but I don't really get it.
Thanks.
Is a self invoked anonymous function:
A self-invoking anonymous runs automatically/immediately when you create it and has no name, hence called anonymous. Here is the format of self-executing anonymous function:
(function(){
// some codeā€¦
})();
https://sarfraznawaz.wordpress.com/2012/01/26/javascript-self-invoking-functions/

why do i need to just '()'" at the end of JavaScript function? [duplicate]

This question already has answers here:
What is the (function() { } )() construct in JavaScript?
(28 answers)
Closed 9 years ago.
I'm trying to understand the difference between :
var x = function () { ....}
(function () { ....} ) ();
I get it that the first function will put the results on x.
that and when exactly the second one will be fired? and why do i need the (); at the end?
This is an example of the Immediately-invoked function expression.
The function is executed immediately because () is how JavaScript calls functions. The syntax might be confuse you, because the function does not have a name, but ( function(){} )() just immediately calls the function with no arguments.

Categories