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

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.

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

javascript multiple parentheses function call [duplicate]

This question already has answers here:
What is 'Currying'?
(23 answers)
Closed last year.
I'm trying to solve some javascript puzzle. I have some function (for example a function which calculates summa).
But there is a weird calling of that function.
example:
sum(5)
or sum(5,6) - should be 11
or sum(5)(6) - should be 11
or sum(1)(2)(3) - should be 6
Have never seen such function calling sum(1)(2)(3)(4)....
Can you please explain or maybe put into documentation where this stuff is explained?
Thanks a lot.
If the sum function returns a value that it can reuse, you can chain the numbers on and on.
If you can share the function it can be easier to explain

How to read function expression syntax in javascript? [duplicate]

This question already has answers here:
How to interpret function parameters in software and language documentation?
(4 answers)
Closed 1 year ago.
function [name]([param1[, param2[, ..., paramN]]]) {
statements
}
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/function
I am confused what and how to read the above function input values, it is a infinite params input, but don't understand the used of brackets[], why put those.
Square brackets represent optional
So it means, that [name] is optional, function declared without name become an anonymous function.
[param1[, param2[,... this means that the parameters are optional, however because they are shown inside previous parameter (all closing ] brackets are at the end of the list) means that you can't have param2, without param1, or param3 without param2 and param1 and so on.

What's the difference between these snippets of code when using React setState() [duplicate]

This question already has answers here:
What do parenthesis surrounding brackets in the return statement of an ES6 arrow function do?
(4 answers)
Closed 2 years ago.
What's the difference between the two?
this.setState((state)=>({
posts: state.posts.filter(post=> post.id !==postRemoved.id)
}))
and
this.setState((state)=>{
posts: state.posts.filter(post=> post.id !==postRemoved.id)
})
setState callback function expects an object to be returned. In the second case, you are just opening a function body without returning anything.
While wrapped with round brackets (), an object is returned instead. That's why the first option will work properly.

With jQuery, what is the point of encapsulating a function in round brackets? [duplicate]

This question already has answers here:
What is the purpose of a self executing function in javascript?
(21 answers)
What is the (function() { } )() construct in JavaScript?
(28 answers)
Closed 8 years ago.
I see this in some HTML files that use jQuery, at the bottom:
(function() {
setTimeout(function(){window.scrollTo(0,0);},0);
})();
What does it mean to put the whole function in round brackets?
The code you gave as example is a self-executing anonymous function.
You can read more about them here.
Relevant text from that article:
What’s useful here is that JavaScript has function level scoping. All variables and functions defined within the anonymous function aren’t available to the code outside of it, effectively using closure to seal itself from the outside world.
(function() {})(); means it is self executing anonymous function. It get called immediately when java script get rendered. More details you can search it.
setTimeout is a function which accepts two parameters:
First parameter: A function
Second parameter: an integer value in milliseconds.
By calling the setTimeout function, the function from first parameter will be called/invoked after the amount of time specified in the second parameter.

Categories