Skip to content

WebDevHubs

  • Home
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • Java
  • Selenium
  • PHP
  • Python
  • Programs
  • Toggle search form

JavaScript Array forEach() Method

Posted on June 16, 2025June 16, 2025 By Admin No Comments on JavaScript Array forEach() Method

JavaScript Array forEach() method iterates over each element in an array and execute a provided callback function once for each element. Unlike some other array methods, forEach() does not return a new array or value—it is used solely for performing operations on each array element.

JavaScript Array forEach() Method

Pre-requisites to Learn

  • JavaScript Array

Syntax

arr.forEach(callback(element, index, array), thisArg);

Parameters

ParameterDescription
callback (Required)A function to execute for each element in the array.
element (Required)The current element being processed in the array.
index (Optional)The index of the current element being processed.
array (Optional)The array forEach() was called on.
thisArg (Optional)A value to use as this when executing the callback function. Defaults to undefined.

Return Value

The forEach() method does not return anything (undefined). It is used for its side effects, such as modifying elements or performing actions.

Notes:

  • It iterates Over Each Element of the array. It executes the callback function for every element in the array.
  • It does not modify the original array but can be used to manipulate array elements via side effects.

Examples of JavaScript Array forEach() Method

Example 1: Printing Array Elements – This method iterates over the array and logs each element to the console.

let arr = [1, 2, 3, 4, 5];

arr.forEach(num => console.log(num));
// Output:
// 1
// 2
// 3
// 4
// 5

Example 2: This method provides access to the index of each element during iteration.

let fruits = ['apple', 'banana', 'cherry'];

fruits.forEach((fruit, index) => {
  console.log(`Index: ${index}, Fruit: ${fruit}`);
});

// Output:
// Index: 0, Fruit: apple
// Index: 1, Fruit: banana
// Index: 2, Fruit: cherry

Example 3: While forEach() doesn’t return a new array, you can use it to modify objects within the array.

let users = [
  { name: 'John', age: 25 },
  { name: 'Alice', age: 30 },
  { name: 'Bob', age: 20 }
];

users.forEach(user => {
  user.isActive = true;
});

console.log(users);

// Output:
// [
//   { name: 'John', age: 25, isActive: true },
//   { name: 'Alice', age: 30, isActive: true },
//   { name: 'Bob', age: 20, isActive: true }
// ]

Example 4: Handling Sparse Arrays – The forEach() method skips over empty slots in sparse arrays.

let sparseArray = [1, , 3, 4];

sparseArray.forEach(num => {
  console.log(num);
});

// Output:
// 1
// 3
// 4

Example 5: Stopping Execution – Unlike for or for…of loops, you cannot break or exit early from a forEach() loop.

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

numbers.forEach(num => {
  if (num > 3) {
    return; // This does not stop the loop
  }
  console.log(num);
});

// Output:
// 1
// 2
// 3

Supported Browsers

BrowserSupport
Chrome1+
Firefox1.5+
Safari3+
Edge12+
Opera9.5+
Internet Explorer9+

Comparison with Other Methods

MethodPurpose
Array.forEach() MethodExecutes a function for each array element but does not return a value.
Array.map() MethodCreates a new array by transforming each element using the callback function.
Array.filter() MethodCreates a new array with elements that satisfy a given condition.
for LoopProvides more control over iteration, including breaking or continuing execution.
JavaScript, Web Technologies Tags:JavaScript-Array, JavaScript-Array-Method, JavaScript-Method

Post navigation

Previous Post: JavaScript Array includes() Method
Next Post: JavaScript Array isArray() Method

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • JavaScript Array Methods
  • JavaScript Array.from() Method
  • JavaScript Array isArray() Method
  • JavaScript Array at() Method
  • JavaScript Array every() Method
  • JavaScript Array filter() Method
  • JavaScript Array find() Method
  • JavaScript Array forEach() Method
  • JavaScript Array includes() Method
  • JavaScript Array indexOf() Method
  • JavaScript Array join() Method
  • JavaScript Array keys() Method
  • JavaScript Array lastIndexOf() Method
  • JavaScript Array map() Method
  • JavaScript Array pop() Method
  • JavaScript Array push() Method
  • JavaScript Array reduce() Method
  • JavaScript Array reduceRight() Method
  • JavaScript Array reverse() Method
  • JavaScript Array shift() Method
  • JavaScript Array slice() Method
  • JavaScript Array some() Method
  • JavaScript Array sort() Method
  • JavaScript Array splice() Method
  • JavaScript Array toLocaleString() Method
  • JavaScript Array toReversed() Method
  • JavaScript Array toSorted() Method
  • JavaScript Array toSpliced() Method
  • JavaScript Array toString() Method
  • JavaScript Array unshift() Method
  • JavaScript Array values() Method
  • JavaScript Array with() Method

Recent Posts

  • Java ArrayList trimToSize() Method
  • Java ArrayList toArray() Method
  • Java ArrayList subList() Method
  • Java ArrayList spliterator() Method
  • Java ArrayList sort() Method

Recent Comments

No comments to show.

Important Pages

  • About Us
  • Contact Us
  • Terms of Use
  • Privacy Policy

Web Development

  • HTML
  • CSS
  • JavaScript
  • PHP

Programming Languages

  • Java
  • Python
  • PHP
  • Programs

Others

  • Selenium
  • Lodash
  • Java ArrayList
  • JavaScript Array Methods

Copyright © 2025 WebDevHubs.

Powered by PressBook Green WordPress theme