What is this function statement? [duplicate] - javascript

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.

Related

setTimeout inside a function [duplicate]

This question already has answers here:
setTimeout(): If not defined in EcmaScript spec, where can I learn how it works?
(2 answers)
Closed 6 months ago.
function delay2sec(){
let name = "abc"
setTimeout(function () {
name = "xyz"
}, 0)
return name
}
console.log(delay2sec())
The output is abc.
Can someone explain why it is not reassigning the name to 'xyz'?
In order to understand this behavior, you need to understand how the event loop works:
Your setTimeout adds an event to the event loop, which will be executed after the current function. Your return statement is evaluated before the current function ends, so the return evaluates the value before the change.

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

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.

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