I was looking at this piece of code in an exercise which returns another function within a function:
var dayName = function() {
var names = ["Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday"];
return function(number) {
return names[number];
};
}();
console.log(dayName(3));
// → Wednesday
However, even if I don't return another function, this alternative code works too:
var dayName = function(number) {
var names = ["Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday"];
return names[number];
};
console.log(dayName(3));
// → Wednesday
Can someone please explain what is the point of the 1st method? And if there is any difference between using the 1st and 2nd method?
Thanks!