LESSON 49 OF 50

Run a callback with forEach

Apply the same function to every array element.

Official sourceECMAScript — Array.prototype.forEachReviewed: 2026-08-26
STEP 1 · LEARN

How it works

forEach calls the callback once for each existing element in ascending index order. It does not build a result array.

Correct example
const services = ['Design', 'Dezvoltare', 'SEO'];
function printService(service) {
  console.log(service);
}
services.forEach(printService);

The function receives and prints each service in turn.

Common mistake
console.log('Design');
console.log('Dezvoltare');
console.log('SEO');

The output is manually imitated and no callback is used.

STEP 2 · APPLY

Your exercise

Declare printService(service), then pass the function to services.forEach.

STEP 3 · RUN

Write and run the code

script.jsJAVASCRIPT
Isolated consoleisolated
Preparing the runtime…
Run the code to see the result.
The draft stays only in this browser.Isolated QuickJS · no DOM, session or network
STEP 4 · CHECK

What needs to pass

0%not checked
  • printService receives service, uses it and prints inside the body.
  • services.forEach receives the printService callback.
  • The output contains the three services in order.
Explain the solution logic

forEach controls iteration, while the function describes the operation for one element.