BlogJavaScriptHigh order function

High order function

  • 함수를 파라미터로 전달 받는다.
function repeatAction(action, times) {
  for (let i = 0; i < times; i++) {
    action();
  }
}
 
repeatAction(() => console.log("Running..."), 3);
// "Running..."
// "Running..."
// "Running..."
  • 함수를 리턴한다.
function createMultiplier(multiplier) {
  return function(num) {
    return num * multiplier;
  };
}
 
const double = createMultiplier(2);
console.log(double(5)); // 10