let list = [4, 5, 6];
for (let i in list) {
console.log(i); // "0", "1", "2",
}
for (let i of list) {
console.log(i); // "4", "5", "6"
}
// EXAMPLE 2
let pets = new Set(["Cat", "Dog", "Hamster"]);
pets["species"] = "mammals";
for (let pet in pets) {
console.log(pet); // "species"
}
for (let pet of pets) {
console.log(pet); // "Cat", "Dog", "Hamster"
}
let myArray = ["a", "b", "c"];
// for...in iterates over properties (like index)
for (let i in myArray){
console.log(i); // → 0, 1, 2
}
// for...of iterates over values
for (let i of myArray){
console.log(i); // → a, b, c
}