.forEach() (JavaScript)

.forEach() runs a function once for every element in an array. It is used to perform an action for each item, not to build a new array.

JavaScript

Unlike .map(), forEach does not return anything — you use it for side effects like logging or updating the page.

If you need a transformed array back, use .map() instead.

Examples

JavaScript

const colors = ["red", "green"];
colors.forEach(c => console.log(c));

Output:

red
green

Learn it in a lesson

See also

Back to the Dictionary