in the following example of functions inside a constant I don't understand the presence of the two sets of round brackets, one containing the main function and the second one just on its own at line 5
1 const increment = (function () {
2 return function increment (number, value) {
3 return number + value;
4 };
5 })();
6 console.log(increment(5,2));
Getting rid of the brackets will not give an error but will print this instead of the result
ƒ increment (number, value) {
return number + value;
}
I tried using just one plain function instead (as opposed to nested ones) and as expected no brackets were needed,
Finally, thinking that maybe the sets of brackets somehow needed to match the number or arguments of the nested function, I tried a similar constant declaration but with three arguments to see whether I needed as many sets of brackets to go with it (example below) but as it turns out it only needed the same brackets as in the original bit.
const sum = (function () {
return function sum1(a,b,c) { return a + b + c};
})();
console.log (sum(1,2,3));
Not sure whether I've missed something obvious or there are syntax rules I'm not aware of. Either way, if anyone can help it would be greatly appreciated.
Thank you