This question doesn’t concern a specific challenge because it appears in many challenges, but I don’t quite understand the logic behind it.
Take for example this:
const sum = (function() {
return function sum(a,b) {
return a+b
};
})();
sum(2,5); // Results 7
Can someone explain what is that? Why is that different than this:
const sum =(function(a,b) {
return a+b;
}
)();
sum(2,5); // I get the following: Uncaught TypeError: sum is not a function
Also, I noticed the second function (the one being returned) has the same name as the const, does that has any significance or something? Is it the same as these 2 cases?
Case1:
const test = (function() {
return function sum(a,b) {
return a+b
};
})();
test(2,5);
Case 2:
const sum = (function() {
return function test(a,b) {
return a+b
};
})();
sum(2,5);
I have seen this function-returns-function-returns-something thing in a few challenges so far and I have no idea what this is and I feel like I’m missing something important here.
Thanks in advance