Codecademy will be down from 00:00 to 02:00 ET on November 15, 2019 for scheduled database maintenance. This will affect the website, learning environment, forums, and mobile app. We will provide updates as necessary.
Cheat Sheets are in BETA. Give us your feedback.

javascript functions assigned to variables

In JavaScript, functions are a data type just as strings, numbers, and arrays are data types. Therefore, functions can be assigned as values to variables, but are different from all other data types because they can be invoked.

let plusFive = (number) => { return number + 5; }; //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

JavaScript callback functions

In JavaScript, a callback function is a function that is passed into another function as an argument. This function can then be invoked during the execution of that higher order function (that it is an arugment of).

Since, in JavaScript, functions are objects, functions can be passed as arguments.

const isEven = (n) => { return n % 2 == 0; } 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.

JavaScript Array .forEach() method

The JavaScript Array .forEach() method executes a callback function on each of the elements in an array in order.

In the given code block, the callback function containing a console.log() method will be executed 5 times, once for each element.

const numbers = [28, 77, 45, 99, 27]; numbers.forEach(number => { console.log(number); });

JavaScript Array .map() method

The JavaScript Array .map() method executes a callback function on each element in an array. It returns a new array made 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 original array.

const finalParticipants = ['Taylor', 'Donald', 'Don', 'Natasha', 'Bobby']; const announcements = finalParticipants.map(member => { return member + ' joined the contest.'; }) console.log(announcements);

JavaScript Array .filter() method

The JavaScript Array .filter() method executes a callback function on each element in an array. The callback function for each of the elements must return either true or false. The returned array is a new array with any elements for which the callback function returns true. In the given code block, the array filteredArray will contain all the elements of randomNumbers but 4.

const randomNumbers = [4, 11, 42, 14, 39]; const filteredArray = randomNumbers.filter(n => { return n > 5; });

JavaScript Array .reduce() method

The JavaScript Array .reduce() method iterates through an array and returns a single value.

It takes a callback function with two parameters (accumulator, currentValue) as arguments. On each iteration, accumulator is the value returned by the last iteration, and the currentValue is the current element. Optionally, a second argument can be passed which acts as the initial value of the accumulator.

In the given code block, the .reduce() method will sum all the elements of the array.

const arrayOfNumbers = [1, 2, 3, 4]; const sum = arrayOfNumbers.reduce((accumulator, currentValue) => { return accumulator + currentValue; }); console.log(sum); // 10