4

I was near the end of the Java Script course for beginners by freeCodeCamp on YouTube, and I am really confused because the guy in the video stored a function inside the variable "half" like following:

const stats = {
  max: 56.78,
  standard_deviation: 4.34,
  median: 34.54,
  mode: 23.87,
  min: -0.75,
  average: 35.85
};
const half = (function () {

  return function half({ max, min }) {
    return (max + min) / 2.0;
  };

})();
console.log(stats);
console.log(half(stats));

my question is, why would we complicate it and store function inside of a variable and then return another function inside of it, when we can just type

function half({ max, min }) {
  return (max + min) / 2.0;
};

are there benefits to his method in this particular situation?

Sourdeezal
  • 55
  • 6
yashichy
  • 53
  • 4
  • 2
    Please do not tag your JavaScript questions with the `java` tag. They are unrelated languages. – khelwood Mar 21 '21 at 21:14
  • [Possible answers](https://www.google.com/search?q=why+return+iife+function+site:stackoverflow.com) – mplungjan Mar 21 '21 at 21:16
  • 7
    There is basically no reason to write this code. Unless it is later expanded on in further lectures. – VLAZ Mar 21 '21 at 21:16
  • 1
    Maybe because he doesn't know it better – cloned Mar 21 '21 at 21:19
  • Although, do note that [`function half()` and `const half = function half()` are different](https://stackoverflow.com/questions/5403121/whats-the-difference-between-function-foo-and-foo-function). The difference still matters little *in this case* but it's still more important than returning a function from an IIFE. – VLAZ Mar 21 '21 at 21:20
  • @cloned or it might be a very gentle intro in closures. I don't think we can really say what the material is being introduced and at what pace. It might just be a bad tutorial, too. Who knows. – VLAZ Mar 21 '21 at 21:23
  • 1
    What is terribly unfortunately in this question is this quote: "_I was near the end of the Java Script course for beginners_" - key words. being [near the end, beginners]. How sad it is be conclude a beginner course with the student perplexed over code provided as part of the instruction. – Randy Casburn Mar 21 '21 at 21:25
  • Check out this Answer on SO https://stackoverflow.com/a/9618572/12045111 – PRABHAT SINGH RAJPUT Mar 22 '21 at 01:17
  • Does this answer your question? [When should I store a function into a variable?](https://stackoverflow.com/questions/9618496/when-should-i-store-a-function-into-a-variable) – PRABHAT SINGH RAJPUT Mar 22 '21 at 01:21

2 Answers2

2

In the particular example above there is no reason, but sometimes you may want to capture some sort of state in a scope local to that function and creating a function in that way lets you encapsulate some scope. Take for example this function:

const someFunction = (function() {
  let someState = 0;
  return function() {
    return someState++;
  }
})();
someFunction(); // returns 0
someFunction(); // returns 1
someFunction(); // returns 2
winhowes
  • 7,845
  • 5
  • 28
  • 39
0

you need to understand the power of a function in js

--- to answer your question there are many benefits, just like art, it gives programmers - it gives us the flexibility to do things the way we want them to be.

---- you will get to use them soon enough as you write more code. don't worry too much for now just understand them.

functions are first-class objects which means they can be:

  • stored in a variable, object, or array.
  • passed as an argument to a function.
  • returned from a function.

two of these happened in the above code - it was:

  • stored in a variable.
  • returned from a function.

this brought us to three types/class of functions

  1. functions stored in variables without a name are called anonymous functions.

An anonymous function is a function without a name.

example:

let show = function () {
    console.log('Anonymous function');
};

show();

or the no-name enclosed function inside half variable in your code above

  1. another part is half variable being an Immediately-invoked Function Expression or self-invoking expression

A self-invoking expression is invoked (started) automatically, without being called.

Function expressions will execute automatically if the expression is followed by ().

e.g

(function () {
  var x = "Hello!!";  // I will invoke myself
})();

or the no-name enclosed function inside half variable in your code above is a self-invoking function.

putting the two together --- half variable above is an anonymous self-invoking expression that means it contains an anonymous self-invoking function

  1. one last thing --- closure and higher-order function.

A closure is a function having access to the parent scope, even after the parent function has closed/called.

that function half can be deemed as a closure. in case there is a variable inside the anonymous self-invoking function pitted to half variable, it will have access to the variable even after half expression has been called.

A higher-order function is a function that takes another function as an input, or returns a function, or does both.

the anonymous self-invoking function can be called an anonymous higher-order function because it return another function.

read more here https://www.freecodecamp.org/news/discover-the-power-of-first-class-functions-fd0d7b599b69/

https://www.w3schools.com/js/js_function_closures.asp

https://www.freecodecamp.org/news/a-quick-intro-to-higher-order-functions-in-javascript-1a014f89c6b/