xxxxxxxxxx
const names = ['Anthony','Stacey','Mason','Gracie','Koda','Nani'];
forEach(function(name) {
console.log(name);
});
xxxxxxxxxx
var colors = ["red", "blue", "green"];
colors.forEach(function(color) {
console.log(color);
});
xxxxxxxxxx
const avengers = ['thor', 'captain america', 'hulk'];
avengers.forEach(element => {
//Statement
console.log(element);
});
xxxxxxxxxx
const scores = [22, 54, 76, 92, 43, 33];
scores.forEach((score) => {
console.log(score);
});
// You can write the above in one line this way:
// scores.forEach((score) => console.log(score));
// will return
// 22
// 54
// 76
// 92
// 43
// 33
xxxxxxxxxx
array.forEach(function calback(item, index, array)
{
/* working codes here */
});
xxxxxxxxxx
// Durations are in minutes
const tasks = [
{
'name' : 'Write for Envato Tuts+',
'duration' : 120
},
{
'name' : 'Work out',
'duration' : 60
},
{
'name' : 'Procrastinate on Duolingo',
'duration' : 240
}
];
const task_names = [];
tasks.forEach(function (task) {
task_names.push(task.name);
});
console.log(task_names) // [ 'Write for Envato Tuts+', 'Work out', 'Procrastinate on Duolingo' ]
xxxxxxxxxx
/* but also carriage return, tab, form feed,
and new line characters. You can think of it as
similar to the character class [ \r\t\f\n\v]*/
let sample = "Whitespace is important in separating words";
let countWhiteSpace = /\s/g; // Change this line
let result = sample.match(countWhiteSpace);