0

I am learning javascript, and I am not able to understand the following piece of code.

const increment = (function() {
  return function test(number, value) {
    return number + value;
  };
})();
console.log(increment(5, 2)); // output 7

My attempt at understanding

I removed the () on line 5, and then ran the following code.

const increment = (function() {
  return function test(number, value) {
    return number + value;
  };
});
console.log(increment(5, 2)); 

This returns the output test(number, value) which I can understand. The increment variable points to the function test.

However, I don't understand how adding () passes the arguments (5,2) to test?

Said another way, if the output of the second code block is test(number, value), the output after adding () is test(number,value)() which seems meaningless.

elexhobby
  • 2,588
  • 5
  • 24
  • 33
  • 2
    That's an IIFE, and it's entirely pointless here. Same as if you had written `const increment = function test(number, value) { return number + value; };` – Bergi Sep 25 '20 at 16:18

3 Answers3

1

It calls self-invoking.

If you write function like that (function foo(){})() then foo will be immediately called and result will be returned.

So as result in increment you have test function.

And then you call it with arguments.

increment(5, 2) // test(5, 2)

Here is some info about that

1

() at the end of the function immediately calls it - it's called IIFE ("Immediately invoked function expression").

When calling it with increment(5, 2) - you are actually calling the test function that is returned by the IIFE.

kind user
  • 40,029
  • 7
  • 67
  • 77
0

Your question is regarding the Javascript Immediate Functions.

First, we need to know what a function is, which is basically a block of code that returns a value.

And to declare a function in Javascript, one way is this:

function a() {
  return "in this case, a string";
}

If we do:

const variable = function () {
  return "in this case, a string";
};

console.log(variable); // Function

But Immediate Functions, as the name implies, are functions that are executed immediately after being defined, so, the value that the function returns is the value that will be given to the variable:

const variable = (function () {
  return "in this case, a string";
})();

console.log(variable); // "in this case, a string"

About removing parentheses:

// This is 
const increment = (function() {
  return function test(number, value) {
    return number + value;
  };
});

// ...the same as this
const increment = function() {
  return function test(number, value) {
    return number + value;
  };
};

We use parentheses just to isolate this expression (which is the function) and execute it right afterwards:

// Now the parentheses make sense, 
// because we need it to execute the function right after it is defined
const increment = (function() {
  return function test(number, value) {
    return number + value;
  };
})();

Read more about Immediate Functions here

Alex Rintt
  • 1,618
  • 1
  • 11
  • 18