Loops
For loop
Section titled “For loop”Syntax
Section titled “Syntax”for (initialExpression; condition; incremetExpression) { statement;}Standard for loop
Section titled “Standard for loop”const fruitBasket = ['banana', 'pear', 'guava'];for (let i = 0; i < fruitBasket.length; i++) { console.log('There\'s a ' + fruitBasket[i] + ' in the basket.');}Negative increment expression
Section titled “Negative increment expression”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.');}For-of loop
Section titled “For-of loop”preferable to the standard for loop
for (let fruit of fruitBasket) { console.log('There\'s a ' + fruit + ' in the basket.');}ForEach loop
Section titled “ForEach loop”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)
Syntax
Section titled “Syntax”array.forEach((currentValue, index, array) => { // your statement here});
// you can omit index and array most of the time:array.forEach(currentValue => { // your statement here})ForEach with an arrow function
Section titled “ForEach with an arrow function”array.forEach(currentValue => console.log(currentValue));All the loops with an example
Section titled “All the loops with an example”// for loopfor (let i = 0; i < fruitBasket.length; i++) { console.log(fruitBasket);};
// for...of loopfor (let fruit of fruitBasket) { console.log(fruit);};
// forEach loopfruitBasket.forEach(function (fruit) { console.log(fruit);});// forEach with an arrow function:fruitBasket.forEach(fruit => console.log(fruit));Excercises
Section titled “Excercises”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 exercisesconst 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 2const firstNames = [];people.forEach(person => { firstNames.push(person.firstName)});console.log(firstNames);
// Excercise 3const diedAfter1950 = [];people.forEach(person => { if (person.yearOfDeath > 1950) { diedAfter1950.push(person); }});console.log(diedAfter1950);
// Excercise 4// if sth. cannot be found, index should be -1let 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); }});