I was following a beginners course of JavaScript and I've come across this code. But I don't understand why sum needs to be an IIFE function. Could you please help me understand how this code works exactly?
const sum = function() {
return function sum(x, y, z) {
const args = [x, y, z];
return args.reduce((a, b) => a + b, 0);
};
}();
console.log(sum(1, 2, 3))
In that function looks like is just to show you how to work the IIFE(the parentheses at the end of the function) that parenthesis is to auto-execute the function, the use is if you want to execute a function when this load you can add the "()" at the end or "+" at the beginning of the clause "function" like this "+function" to see more in details check the following link: