JavaScript Higher order functions

Certainly! Here's a cheat sheet for JavaScript higher-order functions:
map():Transforms each element of an array and returns a new array of the same length.
Example:
const numbers = [1, 2, 3, 4]; const doubledNumbers = numbers.map(function(num) { return num * 2; }); // doubledNumbers: [2, 4, 6, 8]
filter():Creates a new array with only the elements that pass a certain condition.
Example:
const numbers = [1, 2, 3, 4]; const evenNumbers = numbers.filter(function(num) { return num % 2 === 0; }); // evenNumbers: [2, 4]
reduce():Applies a function to each element of an array, resulting in a single output value.
Example:
const numbers = [1, 2, 3, 4]; const sum = numbers.reduce(function(acc, num) { return acc + num; }, 0); // sum: 10
forEach():Executes a function for each element in an array (does not return a new array).
Example:
const numbers = [1, 2, 3, 4]; numbers.forEach(function(num) { console.log(num); }); // Output: 1 2 3 4
sort():Sorts the elements of an array based on a provided comparison function.
Example:
const numbers = [4, 2, 3, 1]; const sortedNumbers = numbers.sort(function(a, b) { return a - b; }); // sortedNumbers: [1, 2, 3, 4]
some():Checks if at least one element in an array satisfies a given condition.
Example:
const numbers = [1, 2, 3, 4]; const hasEvenNumber = numbers.some(function(num) { return num % 2 === 0; }); // hasEvenNumber: true
every():Checks if all elements in an array satisfy a given condition.
Example:
const numbers = [1, 2, 3, 4]; const allPositive = numbers.every(function(num) { return num > 0; }); // allPositive: true
These higher-order functions can be used to simplify your code and perform common operations on arrays. Remember to adjust the provided functions according to your specific use case.
About the author
Software Engineer | JavaScript, React, Redux, Node JS, Express, MongoDB
Comments
Join the discussionNo comments yet. Be the first to comment.




