What Is a Callback Function, and How Does It Work with the forEach Method?
Imagine you have a function that performs a task, and you want to do something after that task is complete. Instead of writing all the code in one big function, you can pass a second function (the callback) to be executed when the first function is done.
let numbers = [1, 2, 3, 4, 5];
numbers.forEach((number, index, array) => {
console.log(`Element ${number} is at index ${index} in array ${array}`);
});
Top comments (0)