Skip to main content

Command Palette

Search for a command to run...

Array method forEach() in javascript

Updated
1 min read
Array method forEach() in javascript
A

I am working as freelancer. I am a experienced full stack web developer. My lovely stack is MERN but I am open for new tech. I love to work with teammates for achieve goals. I love researches to find specific new things.

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: