Student-friendly HTML, CSS, and JavaScript reference with examples
A for-of loop goes through each element in an array, one by one.
for (const item of array) {
// code runs for each item
}
The variable (item) gets each value from the array:
const numbers = [10, 20, 30];
for (const num of numbers) {
console.log(num);
}
// Prints: 10, then 20, then 30
const names = ["Alice", "Bob", "Carol"];
for (const name of names) {
console.log(name);
}
const for the loop variable (it changes each iteration)