Higher Order Functions

forEach for Side Effects

forEach for Side Effects

forEach is a good fit when the observable result is that work happened for each matching item. The callback return value is ignored, so the outer function must track anything it needs to return.

This pattern appears around email senders, analytics calls, cache invalidation, and other APIs where the side effect matters. The callback should be called only for records that qualify, and the function should return its own summary instead of returning the method result.

Your task: Complete sendDigests so it calls sendEmail once for each subscribed user. Pass the user's email and the subject "Weekly digest" to sendEmail. Return the number of emails sent.

Free with a JS Exercises account

Sign in to start coding, run your solution, and keep your progress saved for later. No payment needed.

By continuing, you agree to our Terms and Privacy Policy. We'll send occasional product updates and news. You can unsubscribe anytime.

forEach for Side Effects

forEach is a good fit when the observable result is that work happened for each matching item. The callback return value is ignored, so the outer function must track anything it needs to return.

This pattern appears around email senders, analytics calls, cache invalidation, and other APIs where the side effect matters. The callback should be called only for records that qualify, and the function should return its own summary instead of returning the method result.

Your task: Complete sendDigests so it calls sendEmail once for each subscribed user. Pass the user's email and the subject "Weekly digest" to sendEmail. Return the number of emails sent.

Free with a JS Exercises account

Sign in to start coding, run your solution, and keep your progress saved for later. No payment needed.

By continuing, you agree to our Terms and Privacy Policy. We'll send occasional product updates and news. You can unsubscribe anytime.

Console

Run code to see output.