Skip to content

Loops

for (initialExpression; condition; incremetExpression) {
statement;
}
const fruitBasket = ['banana', 'pear', 'guava'];
for (let i = 0; i < fruitBasket.length; i++) {
console.log('There\'s a ' + fruitBasket[i] + ' in the basket.');
}

if needed … has a better performance

for (let i = fruitBasket.length - 1; i >= 0; i--) {
console.log('There\'s a ' + fruitBasket[i] + ' in the basket.');
}

preferable to the standard for loop

for (let fruit of fruitBasket) {
console.log('There\'s a ' + fruit + ' in the basket.');
}

The forEach loop takes in a callback (here with three arguments):

  • currentValue: refers to the current item in the array
  • index: the position of the item within the array (zero-based index)
  • array: refers to the array you are looping over (can be omitted)
array.forEach((currentValue, index, array) => {
// your statement here
});
// you can omit index and array most of the time:
array.forEach(currentValue => {
// your statement here
})
array.forEach(currentValue => console.log(currentValue));
// for loop
for (let i = 0; i < fruitBasket.length; i++) {
console.log(fruitBasket);
};
// for...of loop
for (let fruit of fruitBasket) {
console.log(fruit);
};
// forEach loop
fruitBasket.forEach(function (fruit) {
console.log(fruit);
});
// forEach with an arrow function:
fruitBasket.forEach(fruit => console.log(fruit));
const people = [
{ firstName: 'Benjamin', lastName: 'Franklin', yearOfDeath: '1790'},
{ firstName: 'Thomas', lastName: 'Edison', yearOfDeath: '1931'},
{ firstName: 'Franklin', lastName: 'Roosevelt', yearOfDeath: '1945'},
{ firstName: 'Napoleon', lastName: 'Bonaparte', yearOfDeath: '1821'},
{ firstName: 'Abraham', lastName: 'Lincoln', yearOfDeath: '1865'},
{ firstName: 'Mother', lastName: 'Theresa', yearOfDeath: '1997'},
{ firstName: 'Mahatma', lastName: 'Gandhi', yearOfDeath: '1948'},
{ firstName: 'Winston', lastName: 'Churchill', yearOfDeath: '1965'},
{ firstName: 'Charles', lastName: 'Darwin', yearOfDeath: '1882'},
{ firstName: 'Albert', lastName: 'Einstein', yearOfDeath: '1995'},
{ firstName: 'Pablo', lastName: 'Picasso', yearOfDeath: '1973'},
{ firstName: 'Ludwig', lastName: 'van Beethoven', yearOfDeath: '1827'},
{ firstName: 'Walt', lastName: 'Disney', yearOfDeath: '1966'},
{ firstName: 'Henry', lastName: 'Ford', yearOfDeath: '1947'},
{ firstName: 'Steve', lastName: 'JObs', yearOfDeath: '2012'},
]
// Test for exercises
const theFirstName = people[0].firstName;
console.log(theFirstName);
// Excercise 1 (all variants)
// for (let i = 0; i < people.length; i++) {
// console.log(people[i].firstName);
// }
// for (let i = people.length - 1; i >= 0; i--) {
// console.log(people[i].firstName);
// }
// for(let person of people) {
// console.log(person.firstName);
// }
people.forEach(person => {
console.log(person.firstName)
});
// Excercise 2
const firstNames = [];
people.forEach(person => {
firstNames.push(person.firstName)
});
console.log(firstNames);
// Excercise 3
const diedAfter1950 = [];
people.forEach(person => {
if (person.yearOfDeath > 1950) {
diedAfter1950.push(person);
}
});
console.log(diedAfter1950);
// Excercise 4
// if sth. cannot be found, index should be -1
let darwinIndex = -1;
people.forEach((person, index) => {
if (person.firstName === 'Charles' && person.lastName === 'Darwin') {
darwinIndex = index;
}
});
console.log(darwinIndex);
// Alternative:
people.forEach((person, index) => {
if (person.firstName === 'Charles' && person.lastName === 'Darwin') {
console.log(index);
}
});