JavaScript Tutorial
JS - Introduction JS - Basic JS - Placement JS - Output JS - Statements JS - Syntax JS - Variables JS - Comments JS - Data Types JS - Operators JS - Operator Precedence JS - Condition JS - Switch JS - Functions JS - Objects JS - Loops For JS - Loops While JS - Break JS - Strings JS - String Methods JS - Numbers JS - Number Methods JS - Arrays JS - Array Methods JS - Array Iteration JS - Events JS - Event Listener JS - Event Propagation JS - Date JS - Math JS - Random JS - Boolean JS - Type Conversion JS - RegExp JS - Exception JS - Scope JS - Debugging JS - Hoisting JS - Strict Mode JS - this Keyword JS - Mistakes JS - Best Practices JS - Performance JS - Form Validation JS - ES6 Features
JS Objects
Object Definitions Object Properties Object Methods Object Constructors Object Prototypes
JS Functions
Function Definitions Function Parameters Function Call Function Apply Function Closures
JS HTML DOM
DOM Introduction DOM Methods DOM Selectors DOM HTML DOM CSS DOM Attributes DOM Navigation
JS Browser BOM
JS - Window JS - Screen JS - Location JS - History JS - Navigator JS - Popup Alert JS - Timing JS - Cookies
JS AJAX
AJAX - Introduction AJAX - XMLHttp AJAX - Request AJAX - Response AJAX - PHP
JS JSON
JSON - Introduction JSON - Syntax JSON - Data Types JSON - Parse JSON - Stringify JSON - Objects JSON - Arrays JSON - PHP JSON - JSONP
JS References
JavaScript Reference JavaScript Methods HTML DOM Reference

JavaScript Array Iteration Methods

Methods that operate on every element in an array, one at a time, known as iteration methods.

Array's iteration methods are closely associated with loops.


Array.forEach()

The forEach() method calls a function (a callback function) for each element in an array.

We can use forEach() to print each element in an array to the document.

Run code

Note that the function takes 3 arguments:

  • The element value (required)
  • The element index (optional)
  • The array itself (optional)

Since 2 arguments (index, array) are optional we can skip them:

Run code

Array.map()

The map() method creates a new array with the results of a function call on each element in the array.

The following example creates a new array from elements with every value multiply by 2:

Run code

Note that the function takes 3 arguments:

  • The element value (required)
  • The element index (optional)
  • The array itself (optional)

Since 2 arguments (index, array) are optional we can skip them:

Run code

Array.filter()

The filter() method creates a new array with the elements that pass the result of a given test.

The following example creates a new array from elements with a value equal or larger than 18:

Run code

Note that the function takes 3 arguments:

  • The element value (required)
  • The element index (optional)
  • The array itself (optional)

Since 2 arguments (index, array) are optional we can skip them:

Run code

Array.reduce()

The reduce() method will reduce an array to a single value.

This is seen commonly with numbers, such as finding the sum of all the numbers in an array.

Run code

Note that the function takes 4 arguments:

  • The initial value / previously returned value (required)
  • The element value (required)
  • The element index (optional)
  • The array itself (optional)

Array.find()

The find() method returns the first value in an array that passes a given test.

In the following example we will find the first element that is equal to or larger than 18:

Run code

Note that the function takes 3 arguments:

  • The element value (required)
  • The element index (optional)
  • The array itself (optional)

Array.findIndex()

The findIndex() method returns the first index in an array that passes a given test.

In the following example we will find the index of the first element that is equal to or larger than 18:

Run code

Note that the function takes 3 arguments:

  • The element value (required)
  • The element index (optional)
  • The array itself (optional)

Array.every()

The every() method tests whether all elements in the array pass the test implemented by the provided function.

The following example checks if all array values are equal to or larger than 18:

Run code

Note that the function takes 3 arguments:

  • The element value (required)
  • The element index (optional)
  • The array itself (optional)

Complete Array Reference

For a complete properties and methods reference, visit our JavaScript Array Reference.

The reference section contains descriptions and examples of all array properties and methods.