Curriculum
Course: JavaScript Basic
Login

Curriculum

JavaScript Basic

JSHome

0/216
Text lesson

JS Array Iteration

Array Iteration Methods

Array iteration methods traverse through every item within the array:

  • Array forEach
  • Array map()
  • Array flatMap()
  • Array filter()
  • Array reduce()
  • Array reduceRight()

See Also:

  • Basic Array Methods
  • Array Search Methods
  • Array Sort Methods
  • Array every()
  • Array some()
  • Array from()
  • Array keys()
  • Array entries()
  • Array with()
  • Array Spread (…)

JavaScript Array forEach()

The forEach() method invokes a function (known as a callback function) once for every element in the array.

Example

const numbers = [45491625];
let txt = “”;
numbers.forEach(myFunction);

function myFunction(value, index, array) {
  txt += value + “<br>”;
}

Note that the callback function takes three arguments:

  • The item value
  • The item index
  • The array itself

In the provided example, only the value parameter is utilized. The example can be restructured as follows:

Example

const numbers = [45491625];
let txt = “”;
numbers.forEach(myFunction);

function myFunction(value) {
  txt += value + “<br>”
}