종우의 컴퓨터 공간

Functions (Higher-order, Callback) 본문

Preparing Interviews/JavaScript Fundamentals

Functions (Higher-order, Callback)

종우공간 2021. 9. 23. 18:38

Background


To fully understand the concept of higher order function, you first have to understand what Functional Programming is and the concept of First-Class Functions.

 

What Is Functional Programming?


In most simple term, Functional Programming is a form of programming in which you pass functions as parameters to other functions and also return them as values. In functional programming, we think and code in terms of functions.JavaScript, Haskell, Clojure, Scala are some of the languages that implement functional programming.

 

What Is First-Class Functions?


If you have been learning JavaScript, you may have heard that JavaScript treats functions as first-class citizens. That is because in JavaScript or any other functional programming languages functions are objects.

In JavaScript, functions are a special type of objects. Below are some examples of usage:

· Adding property to functions like we do with objects: not recommended

· Assigning functions to variables

· Passing functions as parameters

 

What Is Higher Order Functions?


Higher order functions are functions that operate on other functions, either by taking them as arguments or by returning them. In simple words, a higher order function is a function that receives a function as an argument or returns the function as output.

For example, Array.prototype.map, Array.prototype.filter, and Array.prototype.reduce are some of the higher order functions built into the language.

 

What Is Callback Functions?


A callback function is a function that is passed to another function with the expectation that the other function(wrapper) will call it.

elem.addEventListener('click', console.log);

Here, .addEventListener is a higher-order function that takes another function(console.log) which is a callback function.

 

Reference


https://blog.bitsrc.io/understanding-higher-order-functions-in-javascript-75461803bad

 

Understanding Higher-Order Functions in JavaScript

Learn What Higher-Order Functions are and how to use them in JavaScript

blog.bitsrc.io