0 day streak
javascript / beginner
Snippet

Iterating with the Array forEach Method

The .forEach() method executes a provided function once for each array element. It is a functional way to perform an action for every item in a list.

snippet.js
javascript
1
2
3
4
5
const names = ['Anna', 'Ben', 'Clara'];
names.forEach(function(name) {
console.log('Hello, ' + name);
});
nodejs
Breakdown
1
names.forEach(...)
Calls a function for every element in 'names'.
2
function(name)
An anonymous function that receives the current element as 'name'.
3
console.log(...)
Executes for each name: 'Anna', then 'Ben', then 'Clara'.