I am learning javascript and have seen anonymous functions written like this without any explanation:
const sum = (function() {
return function sum(x,y,z){
return (x+y+z);
};
})();
console.log(sum(1,2,3)); //6
The way I read it is: declare a function anonymously and assign it to the const sum. Call sum() with the parameters and console.log() the results.
This is very confusing to me as I don't understand why it is written the way it is. Some questions are:
How is sum accepting parameters when the outermost function has no parameters specified? Why are we not specifying the parameters on the outermost function?? Why is it returning itself and which 'sum' gets called when?
Apologies if it is too dumb or obvious. I have struggled a lot and have finally decided to ask here.