for...of and for...in
for...of loops over the values of an array, string, Set or Map. for...in loops over the property names (keys) of an object. Do not use for...in on arrays.
Object.entries(obj) combined with for...of gives you both key and value.
Example
const colors = ["red", "green", "blue"];
for (const color of colors) {
console.log(color);
}
const user = { name: "Ada", age: 21 };
for (const key in user) {
console.log(key + ": " + user[key]);
}
for (const [k, v] of Object.entries(user)) {
console.log(k, "=", v);
}Exercise
Loop over the values of the array.
for (const item items) {}
Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.