0% found this document useful (0 votes)
20 views

Learn JavaScript - Higher Order Functions and Iterators - Learn JavaScript - Iterators Cheatsheet - Codecademy

This document discusses JavaScript iterators and higher order functions. It explains that functions are first-class objects that can be assigned to variables, passed as arguments to other functions, and returned from functions. It provides examples of callback functions, higher order functions, and the built-in iterator methods like map(), filter(), reduce(), and forEach(). These methods allow iterating over arrays and executing a callback function on each element.

Uploaded by

tony stark
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Learn JavaScript - Higher Order Functions and Iterators - Learn JavaScript - Iterators Cheatsheet - Codecademy

This document discusses JavaScript iterators and higher order functions. It explains that functions are first-class objects that can be assigned to variables, passed as arguments to other functions, and returned from functions. It provides examples of callback functions, higher order functions, and the built-in iterator methods like map(), filter(), reduce(), and forEach(). These methods allow iterating over arrays and executing a callback function on each element.

Uploaded by

tony stark
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

18/10/2023, 11:20 Learn JavaScript: Higher Order Functions and Iterators: Learn JavaScript: Iterators Cheatsheet | Codecademy

Cheatsheets / Learn JavaScript: Higher Order Functions and Iterators

Learn JavaScript: Iterators

Functions Assigned to Variables


In JavaScript, functions are a data type just as strings, let plusFive = (number) => {
numbers, and arrays are data types. Therefore,
return number + 5;
functions can be assigned as values to variables, but are
different from all other data types because they can be };
invoked. // f is assigned the value of plusFive
let f = plusFive;

plusFive(3); // 8
// Since f has a function value, it can
be invoked.
f(9); // 14

Callback Functions
In JavaScript, a callback function is a function that is const isEven = (n) => {
passed into another function as an argument. This
return n % 2 == 0;
function can then be invoked during the execution of
that higher order function (that it is an argument of). }
Since, in JavaScript, functions are objects, functions
can be passed as arguments.
let printMsg = (evenFunc, num) => {
const isNumEven = evenFunc(num);
console.log(`The number ${num} is an
even number: ${isNumEven}.`)
}

// Pass in isEven as the callback


function
printMsg(isEven, 4);
// Prints: The number 4 is an even
number: True.

https://www.codecademy.com/learn/game-dev-learn-javascript-higher-order-functions-and-iterators/modules/game-dev-learn-ja… 1/4
18/10/2023, 11:20 Learn JavaScript: Higher Order Functions and Iterators: Learn JavaScript: Iterators Cheatsheet | Codecademy

Higher-Order Functions
In Javascript, functions can be assigned to variables in
the same way that strings or arrays can. They can be
passed into other functions as parameters or returned
from them as well.
A “higher-order function” is a function that accepts
functions as parameters and/or returns a function.

JavaScript Functions: First-Class Objects


JavaScript functions are first-class objects. Therefore: //Assign a function to a variable
They have built-in properties and methods,
originalFunc
such as the name property and the
.toString() method. const originalFunc = (num) => { return
Properties and methods can be added to them. num + 2 };
They can be passed as arguments and returned
from other functions.
They can be assigned to variables, array //Re-assign the function to a new
elements, and other objects. variable newFunc
const newFunc = originalFunc;

//Access the function's name property


newFunc.name; //'originalFunc'

//Return the function's body as a string


newFunc.toString(); //'(num) => { return
num + 2 }'

//Add our own isMathFunction property to


the function
newFunc.isMathFunction = true;

//Pass the function as an argument


const functionNameLength = (func) => {
return func.name.length };
functionNameLength(originalFunc); //12

//Return the function


const returnFunc = () => { return newFunc
};
returnFunc(); //[Function: originalFunc]

https://www.codecademy.com/learn/game-dev-learn-javascript-higher-order-functions-and-iterators/modules/game-dev-learn-ja… 2/4
18/10/2023, 11:20 Learn JavaScript: Higher Order Functions and Iterators: Learn JavaScript: Iterators Cheatsheet | Codecademy

The .reduce() Method


The .reduce() method iterates through an array and const arrayOfNumbers = [1, 2, 3, 4];
returns a single value.
In the above code example, the .reduce() method will
sum up all the elements of the array. It takes a callback const sum =
function with two parameters (accumulator, arrayOfNumbers.reduce((accumulator,
currentValue) as arguments. On each iteration, currentValue) => {
accumulator is the value returned by the last
return accumulator + currentValue;
iteration, and the currentValue is the current
element. Optionally, a second argument can be passed });
which acts as the initial value of the accumulator.

console.log(sum); // 10

The .forEach() Method


The .forEach() method executes a callback function const numbers = [28, 77, 45, 99, 27];
on each of the elements in an array in order.
In the above example code, the callback function
containing a console.log() method will be executed numbers.forEach(number => {
5 times, once for each element. console.log(number);
});

The .filter() Method


The .filter() method executes a callback function on const randomNumbers = [4, 11, 42, 14,
each element in an array. The callback function for
39];
each of the elements must return either true or
false . The returned array is a new array with any const filteredArray =
elements for which the callback function returns true . randomNumbers.filter(n => {
In the above code example, the array filteredArray return n > 5;
will contain all the elements of randomNumbers but
});
4.

The .map() Method


The .map() method executes a callback function on const finalParticipants = ['Taylor',
each element in an array. It returns a new array made
'Donald', 'Don', 'Natasha', 'Bobby'];
up of the return values from the callback function.
The original array does not get altered, and the
returned array may contain different elements than the // add string after each final
original array.
participant
In the example code above, the .map() method is
used to add ' joined the contest.' string at the end of const announcements =
each element in the finalParticipants array. finalParticipants.map(member => {
return member + ' joined the contest.';
})

console.log(announcements);

https://www.codecademy.com/learn/game-dev-learn-javascript-higher-order-functions-and-iterators/modules/game-dev-learn-ja… 3/4

You might also like