The goal of this tutorial is to provide a comprehensive understanding of higher-order functions in JavaScript. By the end of this tutorial, you will be able to create and use higher-order functions in your code, making your JavaScript implementation more efficient and readable.
Prerequisites: Basic understanding of JavaScript syntax and functions is required.
Higher-order functions are a key concept in functional programming. These are functions that take one or more functions as arguments, return a function, or both. They are a powerful tool that can make your code more abstract, concise, and readable.
In JavaScript, functions are considered "first-class citizens". This means that functions can be assigned to variables, stored in data structures, passed as arguments to other functions, and returned as values from other functions.
// Assigning function to variable
let greet = function() {
console.log("Hello, World!");
};
greet(); // Outputs: Hello, World!
As mentioned, higher-order functions are those that either take a function as an argument, return a function, or both.
// Higher-order function
function higherOrderFunc(callback) {
console.log('From higherOrderFunc');
callback();
}
// Callback function
function callbackFunc() {
console.log('Hello from callbackFunc');
}
// Using the higher-order function
higherOrderFunc(callbackFunc); // Outputs: From higherOrderFunc
// Hello from callbackFunc
Let's look at some practical examples.
map
methodThe map
method is a built-in higher-order function that applies a given function to each item of an array.
let numbers = [1, 2, 3, 4, 5];
let squares = numbers.map(function(num) {
return num * num;
});
console.log(squares); // Outputs: [1, 4, 9, 16, 25]
filter
methodThe filter
method is another built-in higher-order function. It creates a new array with all elements that pass the test implemented by the provided function.
let numbers = [1, 2, 3, 4, 5];
let evens = numbers.filter(function(num) {
return num % 2 === 0;
});
console.log(evens); // Outputs: [2, 4]
By the end of this tutorial, you should have a good understanding of higher-order functions in JavaScript and how to use them to make your code more readable and efficient.
The next step in your learning journey is to understand closure in JavaScript, as it is often used in conjunction with higher-order functions.
Write a higher-order function that takes an array and a callback function, which checks whether a number is odd. The higher-order function should use this callback to return a new array with only the odd numbers.
Write a higher-order function that takes two numbers and a callback. This higher-order function should return the result of the callback, where the callback is a simple arithmetic function (addition, subtraction, etc.).
let numbers = [1, 2, 3, 4, 5];
function filterOddNumbers(arr, callback) {
return arr.filter(callback);
}
let oddNumbers = filterOddNumbers(numbers, function(num) {
return num % 2 !== 0;
});
console.log(oddNumbers); // Outputs: [1, 3, 5]
function calculate(a, b, callback) {
return callback(a, b);
}
let result = calculate(10, 5, function(a, b) {
return a - b;
});
console.log(result); // Outputs: 5