Higher-Order Functions Flashcards
What kind of function gets passed as an argument to another function?
Callback function
What kind of function accepts other functions as arguments?
Higher-order function
Operations started only after preceding ones have completed are
Synchronous
4 ways functions are data
Pass them to other functions
Set them as object properties
Store them in arrays
Set them as variables
Higher-order function that executes a function once for each array element
forEach() var array1 = ['a', 'b', 'c']; array1.forEach(function(element) { console.log(element); });
Higher-order function that creates a new array with the results of calling a provided function on every element in the calling array
map() var array1 = [1, 4, 9, 16]; // pass a function to map const map1 = array1.map(x => x * 2); console.log(map1); // expected output: Array [2, 8, 18, 32]
Higher-order function that creates a new array with all the elements that pass the test implemented by the provided function
filter() var words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];
const result = words.filter(word => word.length > 6);
console.log(result); // expected output: Array ["exuberant", "destruction", "present"]
Higher-order function that executes a provided reducer function on each element of the array resulting in a single output value
reduce() const array1 = [1, 2, 3, 4]; const reducer = (accumulator, currentValue) => accumulator + currentValue;
// 1 + 2 + 3 + 4 console.log(array1.reduce(reducer)); // expected output: 10
// 5 + 1 + 2 + 3 + 4 console.log(array1.reduce(reducer, 5)); // expected output: 15