javascript / beginner
Snippet
Iterating Arrays with the For...Of Loop
The 'for...of' loop provides a clean way to iterate over the values of an iterable object, such as an array. It is often preferred over a traditional for-loop when you only need the value and not the index.
snippet.js
1
2
3
4
5
const colors = ['red', 'green', 'blue'];for (const color of colors) {console.log('Color: ' + color);}
nodejs
Breakdown
1
const colors = [...];
Defines an array of strings.
2
for (const color of colors)
Starts a loop that assigns each value in the array to the variable 'color' one by one.
3
console.log(...)
Prints the current color to the console.