Set Default Parameters for Your Function---------------

Could anyone tell me how this whole code works ,the nested increment function is all i understand , but is const increment = (function()… a function ? I don’t really understand the syntax of how it is working , and how is console.log accessing the parameters .JUST EXPLAIN THE WHOLE THING , I AM CONFUSED

Your code so far


const increment = (function() {
  "use strict";
  return function increment(number, value) {
    return number + value;
  };
})();
console.log(increment(5, 2)); // returns 7
console.log(increment(5)); // returns NaN

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.92 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/es6/set-default-parameters-for-your-functions

This block is a self executing function:

(function() {
  "use strict";
  return function increment(number, value) {
    return number + value;
  };
})()

The first set of parenthesis makes JavaScript evaluate the expression and second parenthesis calls the function with no arguments.
This self-executing function returns another function when it is called.
The code just stores whatever is executed into a constant called increment.

So the entire code is somewhat equivalent to:

const increment = function increment(number, value) {
    return number + value;
 };
1 Like