for in and for of loop in javascript code example
Example 1: for of js
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: javascript for of
const array = ['hello', 'world', 'of', 'Corona'];
for (const item of array) {
console.log(item);
}
Example 3: for in javascript
let items = ['beef', 'cabbage', 'javascript']; // Defines a list of items
for (let item of items) { // Defines for loop with a variable of 'item'
console.log(item); // Prints the item that is found
}
Example 4: javascript for of loop
for (variable of iterable) {
statement
}
Example 5: for javascipt
for (let i = 0; i < 9; i++) {
str = str + i;
}
Example 6: how to use for of in javascript
const array1 = ['a', 'b', 'c'];
for (const element of array1) {
console.log(element);
}