forEach() - Side Effects in JavaScript

From the JavaScript Array Methods cheat sheet · Iteration Methods · verified Jul 2026

forEach() - Side Effects

Execute function for each element (side effects only)

javascript
// Execute function for each element (no return)
[1, 2, 3].forEach(x => console.log(x));

// With index and array
arr.forEach((val, index, array) => {
  console.log(`${index}: ${val}`);
});

// Can't break early (use for...of instead)
💡 Use for side effects (logging, DOM updates)
⚠️ Cannot break/return early like for loop
📌 Returns undefined, not chainable
🔗 Use map() if you need transformed array
⚡ Skips empty slots in sparse arrays

Continue with JavaScript Array Methods

Save the full cheat sheet or work through every related task.

More JavaScript tasks

Back to the full JavaScript Array Methods cheat sheet