Table of contents
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
element: current element is being processed in the array.
index: current index of the current element is being processed in the array.
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: