Code too complex in "Set Default Parameters for Your Functions"?

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?

It is strictly for the tests behind the scenes to test your solution.

So you mean the outer function only pleases the test suite and we don’t need to follow it in our own codes?

OK Got it. Thank you!

SO glad I found this answer! FCC may want to be aware that this can easily confuse newbies. I wasn’t prepared to learn IIFE yet, when the more simple code above worked just fine in the console.

1 Like