-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy path56_higher_order_function.js
29 lines (19 loc) · 1.82 KB
/
56_higher_order_function.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// In JavaScript, a higher - order function is a function that takes one or more functions as arguments, and / or returns a function as its result.In other words, a higher - order function is a function that operates on other functions, either by taking them as inputs or by returning them as outputs.
// Here's an example of a higher-order function:
// function higherOrderFunction(callbackFunction) {
// console.log("Inside higher-order function");
// callbackFunction();
// }
// function callback() {
// console.log("Inside callback function");
// }
// higherOrderFunction(callback);
// In this example, we define a higher - order function called higherOrderFunction that takes a function as an argument, and then logs a message to the console.The function also calls the callback function that was passed in as an argument.
// We also define a callback function called callback that logs a message to the console.
//Finally, we call the higherOrderFunction function and pass in the callback function as an argument.This results in the message "Inside higher-order function" being logged to the console, followed by the message "Inside callback function".
//Higher - order functions are often used for functional programming in JavaScript, where functions are treated as first - class citizens and can be passed around like any other data type.Higher - order functions can be used to create more flexible and reusable code, and can be used for a variety of purposes, such as:
// Implementing callbacks and event handlers
// Performing transformations on data using functions like map, reduce, and filter
// Creating closures and private variables
// Implementing currying and partial application of functions
// Some examples of higher - order functions in JavaScript include Array.prototype.map, Array.prototype.filter, and setTimeout.