of and in javascript code example
Example 1: for of vs for in javascript
let list = [4, 5, 6];
for (let i in list) {
console.log(i);
}
for (let i of list) {
console.log(i);
}
let pets = new Set(["Cat", "Dog", "Hamster"]);
pets["species"] = "mammals";
for (let pet in pets) {
console.log(pet);
}
for (let pet of pets) {
console.log(pet);
}
Example 2: forin js
let panier = ['fraise', 'banane', 'poire'];
for (const fruit in panier) {
console.log(panier[fruit]);
}
Example 3: for in and for of in js
const iterable = [10, 20, 30];
for (const value of iterable) {
console.log("fef"+value);
}