Loops: The `for-in` loop

for..in, which should not be confused with for..of, can be used to iterate over the enumerable properties of an object:

const dog = { name: 'Roger', color: 'gray' }

for (let property in dog) {
  console.log(property) // 'name' in the first iteration
                        // 'color' in the second

  console.log(dog[property]) // 'Roger' in the first iteration
                             // 'gray' in the second
}

Since arrays are a special kind of object, we can iterate over the items in an array too:

const dogs = ['Roger', 'Vanille']

for (let index in dogs) {
  console.log(dogs[index])
}

Lessons in this unit:

0: Introduction
1: The `for` loop
2: The `do-while` loop
3: The `while` loop
4: The `for-of` loop
5: ▶︎ The `for-in` loop
6: Other kinds of loops
Are you intimidated by Git? Can’t figure out merge vs rebase? Are you afraid of screwing up something any time you have to do something in Git? Do you rely on ChatGPT or random people’s answer on StackOverflow to fix your problems? Your coworkers are tired of explaining Git to you all the time? Git is something we all need to use, but few of us really master it. I created this course to improve your Git (and GitHub) knowledge at a radical level. Launching May 21, 2024. Join the waiting list!