Array method forEach() in javascript

Array method forEach() in javascript

Photo by Elsa Noblet on Unsplash

Array methods were introduced in javascript in ES5. Before this, every array iteration was performed by loops. But after the introduction of array methods to perform array operations, work on an array has become very easy without the implementation of extra coding.

Here we will discuss some array methods.

Why use forEach()?

It targets every element of the array to perform operations.

syntax

array.foreach(callbackfunction(element,index,array)=>statements);

parameters

callbackfunction

A function that executes elements of the array.

arguments

  1. element: current element is being processed in the array.

  2. index: current index of the current element is being processed in the array.

  3. array: This is the main array on which this method is being performed.

Return Value

Undefined.

Example

We will make an array of colours and print them from forEach() method.

Output: