parentheses before function syntax js meaning [duplicate] - javascript

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.

Related

JavaScript anonymous function syntax? [duplicate]

This question already has answers here:
Why do I have to omit parentheses when passing a function as an argument?
(2 answers)
JS: What's the difference between a ! closure and () closure? [duplicate]
(2 answers)
Closed 6 months ago.
I have a question. Why when I'm using anonymous function and I want to call it immediately i have to do like that:
(() => {console.log('something')})()
But when I want to use it with setTimeout for example, I don't have to use '(' at beginning and '()' at the end?
setTimeout(() => {console.log('something')}, 2000)
You pass the function as a parameter to setTimeout, rather than calling it right away. That's why you don't need the () at the end

What is the name of this structure in JavaScript? [duplicate]

This question already has answers here:
What does (function($) {})(jQuery); mean?
(6 answers)
What is the (function() { } )() construct in JavaScript?
(28 answers)
Closed 5 years ago.
I see in the jQuery code this structure but I don't know the name and how use correctly.
(function($) {
...
})(jQuery);
I suppose it's a form of past arguments to the function.
this will execute the function immediately (immediately invoked)

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.

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

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

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