- Execute a callback function for each element of a collection while maintaining the original order.
- It differs from other methods by always returning undefined, focusing on side effects.
- It supports arrays, DOM NodeLists, Maps, and Sets, offering a cleaner syntax than the traditional for loop.
If you're getting into web development, you've probably noticed that moving data within lists is a daily task. The `forEach()` method is one of those lifesavers, as it allows you to iterate through each element of an array and perform actions on it without having to wrestle with manual indices.
Unlike older, somewhat cumbersome loops, this feature takes a more declarative approach. Essentially, you tell JavaScript, "Hey, do this to everything you find in here," and the language handles the rest, making the code much more readable and less prone to those common errors of skipping over an item at the end of a list.
What exactly is forEach and how does it work?
In simple terms, `forEach` is a high-order method that executes a specific function once for each member of an array, strictly following the order of the elements. The most important thing to remember is that it always returns `undefined` . This means it doesn't create a new array; its purpose is to execute actions, what we in programming call side effects, such as printing data to the console or modifying the DOM.
Callback syntax and parameters

To get started, we pass a function as an argument. This function can receive up to three very useful parameters: the current element being processed, the numeric index of that element, and the complete array being worked on. Although you can use classic anonymous functions, nowadays it's more common to use Arrow Functions , which make the code much cleaner and more modern.
Comparison: forEach versus other alternatives
It's very common to confuse `forEach` with other similar methods, but each has its purpose. If your intention is to transform the data and obtain a new list with the results, ` map()` is ideal . On the other hand, if you want to filter elements and keep only those that meet a condition, go directly to `filter()` . If you need to sum all the values in a compact way, `reduce()` is the most powerful option.
Compared to the traditional for loop, forEach eliminates the need to declare counter variables, thus avoiding the famous off-by-one error , where the program tries to access an index that does not exist or skips the first one.
Use cases and practical examples

We can use this method for a wide variety of tasks. For example, if we have a price list, we can iterate through it to sum the totals in an external variable. It's also ideal for updating objects within an array; although `forEach` doesn't change the array itself, the function we pass to it can modify the properties of the objects contained within it.
In the browser environment, it's extremely useful for interacting with the DOM. When we use `document.querySelectorAll()` , we get a NodeList that also supports `forEach`, allowing us to apply styles or change the text of multiple HTML elements at once very quickly.
Important Limitations and Warnings
It's not all sunshine and rainbows, and there are a couple of things you should keep in mind to avoid breaking your application. First, `forEach` doesn't allow you to break the loop ; that is, you can't use `break` or `continue`. If you need to stop the iteration prematurely, you'll have to resort to a `for...of` loop or the `some()` method.
Another critical point is the immutability of the collection . Attempting to add or remove elements from the array while iterating through it with `forEach` can lead to erratic behavior or bugs that are difficult to track down. It's also important to know that this method doesn't wait for promises , so if you're working with asynchronous code (async/await), the `for...of` loop is the way to go.
Iterating beyond arrays

Although we usually associate it with lists, `forEach` is also present in other structures. Sets and Maps implement it natively. In the case of Maps, the function receives the value first and then the key. If you have a regular object, remember that these don't have `forEach`, but you can work around this by first converting it into an array using `Object.entries()`.
Mastering this tool allows you to write more elegant and efficient scripts, facilitating data manipulation in both server logic and user interface, as long as you remember that its purpose is to execute actions and not transform collections into new structures.
FAQ
What is the main difference between forEach and map?
The forEach method is used to execute actions and always returns undefined, while map creates and returns a new array with the transformed elements.
Can you stop the execution of a forEach loop with break?
It is not possible to interrupt a forEach loop using break or continue; to achieve that control it is recommended to use for...of or the some() method.
What happens if the array has empty positions?
The forEach method automatically ignores gaps or empty positions within an array during its traversal.
How can you use forEach with a JavaScript object?
Since objects do not have this method, they must first be converted into an array using Object.entries() in order to iterate over their properties.
Is forEach compatible with asynchronous functions?
This method does not wait for promises to resolve, so to handle asynchronous code with await it is preferable to use a for...of loop.
What parameters does the forEach callback function receive?
The function can receive the current element of the array, the index of that element, and the entire array being iterated.