High order function.(pls explain how it work)

var val = f(arg); - I do not understand this part.
Thank u for explanation.

1 Like

try calling your function like

var r1 = noisy(Boolean);
var r2 = r1(0);

You will know what’s going on here.

f is like this:

const forEach=(array,action)=>{
for(let i=0; i<array.length;i++)

They’re called higher-order functions.

Higher-order functions are like supervisors: they’re useless by themselves. Their work makes sense only when combined with another person.

Andrew Noisy (aka noisy) is a supervisor of Tom Boolean (aka Boolean).
That’s how Noisy & Boolean team looks like:

Mr noisy ebraces Mr Boolean

Their team’s trademark: noisy(Boolean)

Andrew likes to yell. A lot.
When Tom does something, Andrew never misses the chance to inform the rest of the world what he’s doing.

When Tom takes a wheel to inspect it, Andrew yells “Tom is taking a wheel: 0”.
noisy(Boolean)(0) // *calling with 0*

When Tom finishes an inspection, Andrew yells: "Tom took the wheel: 0 and put in into the ‘faulty’ stack.
// called with 0 - got false


Two things to note, functions can be stored in variables:

var someVariable = function(param) { return param + 1;}
console.log( someVariable(2) )   // 2 + 1 = 3

and functions can be passed as parameters to other functions:

var someVariable = function(param) { return param + 1;}

function otherFunction( param ){
  return param(2)
console.log( otherFunction( someVariable ) )   // 2 + 1 = 3

This statement: var val = f(arg); says call the function f and store it in the variable val so we can console.log() it before returning it.

The first time you call the function noisy it returns another function into a variable. Then you call the new function stored in a variable and it outputs a result.

noisy(Boolean)(0) is shorthand for call the function with the parameter Boolean and then immediately call the resulting function with 0.

Edit: Also, in the future, please dont take screenshots of text :slight_smile:

1 Like

lmao! The best explanation I’ve ever read in a while! :smiley: