This question already has answers here:
What do empty parentheses () after a function declaration do in javascript? [duplicate]
(4 answers)
Closed 3 years ago.
I am learning JavaScript from freecodecamp and there is a function that I don't understand the meaning of the extra (), I will attach the code, sorry if this already been asked before
I know that I need to the () in order to get the outcome but I cannot explain the reason behind it
const sum = (function() {
return function sum(...args) {
return args.reduce((a, b) => a + b, 0);
};
})(); //what I am asking is (func....)(); what is the reason behind those 2 ()?
console.log(sum(1, 2, 3, 4));
The outcome is 10, which I understand the function, I just don't understand the meaning behind it
It's an IIFE (Immediately Invoked Function Expression) - it's a JavaScript function that runs as soon as it is defined.
https://developer.mozilla.org/en-US/docs/Glossary/IIFE
Related
This question already has answers here:
How do JavaScript closures work?
(86 answers)
Higher-order functions in Javascript
(5 answers)
Understanding Higher Order functions in Javascript
(2 answers)
Closed 3 years ago.
I am practising ES6 and I have this code:
const over = (...fns) => (...args) =>
fns.map(fn => fn.apply(null, args));
const minMax = over(Math.min, Math.max);
console.log(minMax(1, 2, 3, 4, 5));
console.log(minMax(1, 2, 5, 4, 3));
console.log(minMax(1, 2, 5, -4, 3));
The goal is to get the minimum and the maximun values between the numbers passed as arguments.
I could understand almost everything, the dynamic is very clear, with one exception, I know that args refers to the parameters coming from minMax(), but I couldn't get how the code recognize it.
My guess is: since we have two functions, over() and minMax(), when called, they are automatically read in this order, that is why the code knows that the first anonymous function refers to over() and the second one to minMax(). But this is just a guess, I don't know if I am right.
What is exactly happening here?
This question already has answers here:
How do JavaScript closures work?
(86 answers)
Closed 7 years ago.
In the Eloquent Javascript book I came across this code.
I understood how this works and the passing of arguments but what I am unable to understand is author's statement regarding this code that it's a function which can create another function!
My question is: How is it creating a new function? What is happening which the author is calling creation of a new function? I mean sure we are creating a function called greaterThan and it has another function in it but I can't see how greaterThan is creating another function!
I assure you I have read many similar Qs before asking but couldn't find the answer I am looking for. Thank you for your time & help.
function greaterThan(n) {
return function(m) {
return m > n;
};
}
var greaterThan10 = greaterThan(10);
console.log(greaterThan10(11));
// → true
The function is being created on the sixth line.
var greaterThan10 = greaterThan(10);
This creates a function greaterThan10 which can be used to check if numbers are greater than 10. You can see it used on line 7.
Edit:
When the function greaterThan is called on line 6, it returns the nested function, effectively making
greaterThan10 = function(m){
return m > 10;
};
The author was calling greaterThan10 a 'new function' created by the function greaterThan.
This question already has answers here:
Location of parenthesis for auto-executing anonymous JavaScript functions?
(4 answers)
Closed 7 years ago.
I often use the closure syntax
var something = (function () {
//TODO: do something
}());
and, I often find people use this syntax
var something = (function () {
//TODO: do something
})();
If both the two behaves the same way, what are the differences between the two?
There's no real difference. Both statements contain function expressions that evaluate to functions that are immediately executed.
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.
This probably is not a new question, but where is the purpose of wrapping a function or codes inside ((function () {...})());? for instance,
//Self-evoking anonymous functions
((function () {
alert("hi");
})());
What's the difference with no wrap,
alert("hi");
I still get the same result - hi
What can you pass/ put in the brackets in the end bit - })());? and why?
Using a function creates a scope. You can have params inside and do more than just alerting.
Now you can do the same without a function, but then you will keep the state on the window object and thats something that you would like to prevent in some cases.
This question already has answers here:
JavaScript plus sign in front of function expression
(4 answers)
Closed 9 years ago.
I found a strange behaviour in Javascript:
function() {
return 10;
}();
This construction doesn't work on all browser because it has a syntax error. But this construction works (returns ten):
+function() {
return 10;
}();
Why?
The + lets the js engine make the difference between this function expression and a function definition.
For more readability, we usually use
(function() {
return 10;
})();
See related article