@JakobLind/Higher-order functions
JavaScript

An example of higher-order functions in JavaScript

I built🔨
THIS
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// 1. Takes one or many functions as arguments
function twice(f, v) {
  return f(f(v));
}

function add3(v) {
  return v + 3;
}
const result = twice(add3, 1);
console.log("Result is ", result);


// 2. Returns a function
function addX(x) {
  return function (y) {
    return x + y;
  }
}

const add1 = addX(1);
const res = add1(5); // will return 6
console.log("Result is ", res);
Native Browser JavaScript