for of vs for 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: Flutter For In loop explained
void main() {
List subjects = ['Math', 'Biology', 'Economic', 'History', 'Science'];
for (String sName in subjects) {
print(sName);
}
}
Example 3: for in and for of in js
Object.prototype.objCustom = function () {};
Array.prototype.arrCustom = function () {};
let iterable = [3, 5, 7];
iterable.foo = "hello";
for (let i in iterable) {
console.log(i);
}
for (let i of iterable) {
console.log(i);
}
Example 4: for of vs for in
for (variable in enumerable) {
}
Example 5: for of vs for in
const array = ['a', 'b', 'c', 'd'];
for (const index in array) {
console.log(array[index])
}