Key Concepts

Review core concepts you need to learn to master this subject

javascript functions assigned to variables

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

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.

Javascript Higher-Order Functions

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

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 callback functions

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

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.

JavaScript Array .forEach() method

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

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.

JavaScript Array .map() method

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

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.

JavaScript Array .filter() method

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

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.

JavaScript Array .reduce() method

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

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.

Chevron Left Icon
Higher-Order Functions
Lesson 1 of 2
Chevron Right Icon
  1. 1
    We are often unaware of the number of assumptions we make when we communicate with other people in our native languages. If we told you to “count to three,” we would expect you to say or think the …
  2. 2
    JavaScript functions behave like any other data type in the language; we can assign functions to variables, and we can reassign them to new variables. Below, we have an annoyingly long function n…
  3. 3
    Since functions can behave like any other type of data in JavaScript, it might not surprise you to learn that we can also pass functions (into other functions) as parameters. A _higher-order functi…
  4. 4
    Great job! By thinking about functions as data and learning about higher-order functions, you’ve taken important steps in being able to write clean, modular code and take advantage of JavaScript’s …
  1. 1
    Imagine you had a grocery list and you wanted to know what each item on the list was. You’d have to scan through each row and check for the item. This common task is similar to what we have to do w…
  2. 2
    The first iteration method that we’re going to learn is .forEach(). Aptly named, .forEach() will execute the same code for each element of an array. The code above will log a nicely formatted …
  3. 3
    The second iterator we’re going to cover is .map(). When .map() is called on an array, it takes an argument of a callback function and returns a new array! Take a look at an example of calling .map…
  4. 4
    Another useful iterator method is .filter(). Like .map(), .filter() returns a new array. However, .filter() returns an array of elements after filtering out certain elements from the original array…
  5. 5
    We sometimes want to find the location of an element in an array. That’s where the .findIndex() method comes in! Calling .findIndex() on an array will return the index of the first element that eva…
  6. 6
    Another widely used iteration method is .reduce(). The .reduce() method returns a single value after iterating through the elements of an array, thereby reducing the array. Take a look at the ex…
  7. 7
    There are many additional built-in array methods, a complete list of which is on the MDN’s Array iteration methods page . The documentation for each method contains several sections: 1. A shor…
  8. 8
    There are many iteration methods you can choose. In addition to learning the correct syntax for the use of iteration methods, it is also important to learn how to choose the correct method for diff…
  9. 9
    Awesome job on clearing the iterators lesson! You have learned a number of useful methods in this lesson as well as how to use the JavaScript documentation from the Mozilla Developer Network to dis…

What you'll create

Portfolio projects that showcase your new skills

Pro Logo

How you'll master it

Stress-test your knowledge with quizzes that help commit syntax to memory

Pro Logo