# Struggling to understand the difference between anonymous and otherwise in higher order functions?

I know this is probably quite a simple problem but I’ve been trying to understand higher order functions for a few days now and just can’t wrap my head around it. I understand that the top block is using an anonymous function whereas the bottom one is using a named variable as the inner function. What’s the difference between when you’d need to use these, and why can’t the bottom one just look like this:

const takingAway (num1, num2) {
return num1 - num2;
}

?? Thank you so much in advance!
//----------------------------------------------
console.log(‘Im trying to learn higher order functions and finding it really hard I would love some help!’);

const subtraction = (num1) => {
return function (num2) {
return num1 - num2;
}
}

console.log(subtraction(3)(2));
//output : 1

// SECOND VERSION

const takingAway = (num1) => {
const nextStep = (num2) => {
return num1 - num2;
}
return nextStep;
}

console.log(takingAway(3)(2));
// output : 1

Using `console.log(subtraction(3)(2));` doesn’t make all that much sense in context. But an instance of `subtraction(3)` might.

Quick example:

``````const subtraction = (num1) => {
// creating a subtraction instance
console.log(`subtraction: creating a function where \${num1} will always be subtracted from`);
// note, num1 is captured here
// this capturing of a passed variable is called a closure
return num2 => num1 - num2;
};

console.log(subtraction(3)(2));

const takingAway = (num1) => {
console.log(`takingAway: creating a function where \${num1} will always be subtracted from`);
// storing the function into a variable
const nextStep = (num2) => num1 - num2;
console.log(`return the value \${nextStep} that we've stored in a variable`);
return nextStep;
}

console.log(takingAway(3)(2));

// the value in this comes from the capture part
const sub5 = subtraction(5);

console.log(`sub5 is \${sub5}`);

// now I can use function sub5
console.log(sub5(2));
console.log(sub5(12));
``````

It’s a tool and it this point it’s only important to understand how it works

If you don’t see any use cases for it now, that’s fine. In the future, as complexity grows, you might.

1 Like

So the two higher-order functions it has you create are pretty trivial, but that’s essentially the point of their style of education. They’re teaching you higher-order functions in a simple way, but when you add complexity, that’s when these things actually become useful.

Most of the time, the most useful higher-order functions you’ll encounter in daily programming are the ones already built into modern JavaScript, like .map, .forEach, .filter, and .reduce. They can take other functions as their parameters and even return functions as their results (note: forEach does not return anything, but it can take in functions as its parameter).

To answer your question, for such a simple subtracting function, you’d probably do it the way you suggested because only being able to subtract from a single number is likely useless. However, as baavgai noted, this is merely there to teach you the concept. You will later learn when and how this concept can be truly useful in actual programming projects.