Note the code given in this problem
const increment = (function() {
"use strict";
return function increment(number, value = 1) {
return number + value;
};
})();
console.log(increment(5, 2)); // returns 7
console.log(increment(5)); // returns NaN
To define increment
, it actually defines a function and immediately invokes it to assign the return value of this function (which is another function) to increment
. What is the point of this and the difference between
const increment = function (number, value = 1) {
return number + value;
};
;
I have seen some posts discussing about this simpler version, but haven’t found a satisfying answer why freecodecamp use the former complex syntax, in other words, is this complex version somehow a good practice or can it guarantee or prevent something?