Learn JavaScript: Iterators

In this course, you will learn how to use iterator methods to simplify the process of looping over arrays.

Start[missing "en.views.course_landing_page.introduction-to-javascript.course_illustration" translation]

Key Concepts

Review core concepts you need to learn to master this subject

JavaScript Array .forEach() method

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

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

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

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

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

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

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

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 [...] . Aptly named, [...] will execute the same code for each element of an array. The code above will log a nicely formatted list o...

  3. 3

    The second iterator we're going to cover is [...] . When [...] 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 [...

  4. 4

    Another useful iterator method is [...] . Like [...] , [...] returns a new array. However, [...] returns an array of elements after filtering out certain elements from the original array. The...

  5. 5

    We sometimes want to find the location of an element in an array. That's where the [...] method comes in! Calling [...] on an array will return the index of the first element that evaluates to ...

  6. 6

    Another widely used iteration method is [...] . The [...] method returns a single value after iterating through the elements of an array, thereby reducing the array. Take a look at the exampl...

  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

Learn JavaScript: Iterators

Start[missing "en.views.course_landing_page.introduction-to-javascript.course_illustration" translation]