ScanSkill

forEach

The forEach() method is used to iterate over each element of an array.

Syntax

array.forEach()

 

Example

const numbers = [1,2,3,4,5];
number.forEach((element) => {
  console.log(element);
});
/* Prints
  1
  2
  3
  4
  5
*/

const arrayObject = [{id: 1},{id: 2}];
arrayObject.forEach((element) => {
  console.log(element.id);
});
/* Prints
	1
	2
*/

We can see that, the forEach() loops over each element of the provided array.