Skip to content

Latest commit

 

History

History
34 lines (30 loc) · 963 Bytes

File metadata and controls

34 lines (30 loc) · 963 Bytes

JavaScript forEach

Description

The forEach method is used to iterate over elements in an array and execute a provided function once for each array element.

Syntax

array.forEach(callback(currentValue [, index [, array]]) {
  // Your code here
}, thisArg);

Parameters

callback: Function to execute for each element.
currentValue: The current element being processed in the array.
index (optional): The index of the current element being processed in the array.
array (optional): The array forEach was called upon.
thisArg (optional): Object to use as this when executing the callback.

Example

const numbers = [1, 2, 3, 4, 5];

numbers.forEach(function (num, index) {
  console.log(`Element at index ${index}: ${num}`);
});

Output :

Element at index 0: 1
Element at index 1: 2
Element at index 2: 3
Element at index 3: 4
Element at index 4: 5