Sikau

JavaScript · Day 32 · about 25 min

Looping through arrays

Visiting every item.

Arrays + loops = superpower

A teacher calling the register goes through every name, one by one. Loops and arrays were made for each other — let's visit every item in a list.

for…of: the friendly way

const students = ["Asha", "Bikash", "Chandra"];

for (const name of students) {
  console.log("Present: " + name);
}

If you need the position as well, use a counting for loop up to length:

const students = ["Asha", "Bikash", "Chandra"];

for (let i = 0; i < students.length; i++) {
  console.log(`${i + 1}. ${students[i]}`);
}

The rest of this lesson

  • Patterns with arraysexplain
  • Choosing the right loopexplain
  • Predict the outputpredict
  • Count the long wordspredict
  • Your turnwrite code
  • Challengechallenge
  • Badge quizquiz
  • What you learnedrecap

Continue this lesson free — run real code in your browser

Continue with Google

See all 68 lessons of JavaScript