What's the difference in these two?

Tell us what’s happening:

Sorry, I don’t speak English very well.

// why write?
const increment = (function() {
  "use strict";
  return function increment(number, value) {
    return number + value;
  };
})();

// instead of
const increment = function(number, value) {
    "use strict";
    return number + value;
};

Your browser information:

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

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

Second one will make a function named increment, that you could want to invoke later.
First one is an IIFE,(Immediately Invoked Function Expression) a self invoking function that would assign the value of its result to the const increment.

1 Like

An IIFE (Immediately Invoked Function Expression) is a JavaScript function that runs as soon as it is defined.

(function () {
    statements
})();

there is no difference, but it is better to create the separate function. Having a separate function allows you to call it from multiple places, but only define it one time, and make your code more readable

1 Like