Search⌘ K
AI Features

Tip 24: Apply Consistent Actions with forEach()

Understand how to use the forEach method to perform consistent actions on each element of an array in JavaScript. Learn when to use forEach for side effects like sending emails, ensuring predictable and chainable array operations without mutating data.

Operations on the array values

Things are going to get a little different in this tip. The two array methods you’ve explored so far return a new, altered array. You either changed the shape by pulling out a subset of information on each item, or you changed the size by returning only part of the total number of items.

In this tip, you aren’t changing the input array at all. Instead, you’re going to perform an action on every member. This is common when you finally get an array to the size and shape you want and then you want to do something with that data.

Example

As an example, say you have a club with a group of members and you want to write a script to send an invitation to every club member when the next meeting is scheduled. You want a function that takes ...