 for...of and forEach
for...of and forEach
Imagine you have a grocery list and you want to check every single item to make sure nothing is missing. You would start at the top of the list and look at each item one by one until you reach the end. In programming, when you want to process every item in a list—like an array of fruits—JavaScript gives you two handy tools: the for...of loop and the forEach method. Both help you visit each element in an array, but they work in slightly different ways.
12345const fruits = ["apple", "banana", "cherry"]; for (const fruit of fruits) { console.log("I need to buy " + fruit); }
12345const fruits = ["apple", "banana", "cherry"]; fruits.forEach(function(fruit) { console.log("I need to buy " + fruit); });
Kiitos palautteestasi!
Kysy tekoälyä
Kysy tekoälyä
Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme
Awesome!
Completion rate improved to 7.69 for...of and forEach
for...of and forEach
Pyyhkäise näyttääksesi valikon
Imagine you have a grocery list and you want to check every single item to make sure nothing is missing. You would start at the top of the list and look at each item one by one until you reach the end. In programming, when you want to process every item in a list—like an array of fruits—JavaScript gives you two handy tools: the for...of loop and the forEach method. Both help you visit each element in an array, but they work in slightly different ways.
12345const fruits = ["apple", "banana", "cherry"]; for (const fruit of fruits) { console.log("I need to buy " + fruit); }
12345const fruits = ["apple", "banana", "cherry"]; fruits.forEach(function(fruit) { console.log("I need to buy " + fruit); });
Kiitos palautteestasi!