JavaScript anonymous function syntax? [duplicate] - javascript

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

Related

What is this function statement? [duplicate]

This question already has answers here:
ES6 immediately invoked arrow function
(4 answers)
Closed last year.
What does this do? Is this something like an empty function? Can't find any information on this, in what use case this is used?
(() => {
console.log("test");
})
If it had another set of parenthesis at the end, it would be a self executing function
(() => {
console.log("test");
})()
but as is, it does nothing.

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.

Javascript: What is function with two parenthesis brackets ()()? [duplicate]

This question already has answers here:
Two sets of parentheses after function call
(4 answers)
What is 'Currying'?
(23 answers)
Closed 5 years ago.
I recall somewhere seeing a function with two parenthesis brackets ()() like:
function add_numbers(number1)(number2)
What do you call such a function and what’s its usage?
Thank you in advance and will be sure to vote up/accept answer
It is called function currying. The first bracket returns another function(lets call it: "myCustomFunc"). The 2nd bracket actually passes the 2nd value (number2) to the myCustomFunc.
Going off of this answer, the add_numbers function takes one argument (number1) and returns a function, which is then called with argument number2.

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