xxxxxxxxxx
// hobbies array
const hobbies = ['singing', 'eating', 'quidditch', 'writing'];
// const hobby of hobbies is just suggesting item of items, a item out of items
for (const hobby of hobbies) {
console.log(`I enjoy ${hobby}.`);
}
xxxxxxxxxx
const array1 = ['a', 'b', 'c'];
for (const element of array1) {
console.log(element);
}
// expected output: "a"
// expected output: "b"
// expected output: "c"
xxxxxxxxxx
const array = ['hello', 'world', 'of', 'Corona'];
for (const item of array) {
console.log(item);
}
xxxxxxxxxx
let fruits = ["apple", "pear", "plum", "orange", "cherry"];
for(let fruit of fruits)
{
console.log(fruit);
}
xxxxxxxxxx
const people = [{ name: 'Karl', location: 'UK' },
{ name: 'Steve', location: 'US' }];
for (const person of people) {
console.log(person.name); // "karl", then "steve"
console.log(person.location); // "UK", then "US"
}
xxxxxxxxxx
const iterable = [10, 20, 30];
for (const value of iterable) {
console.log(value);
}
// 10
// 20
// 30
xxxxxxxxxx
let arr = ["a", "b", "c"]
for (let i in arr){
console.log(i) // 0, 1, 2
}
for(let i of arr){
console.log(i) // a, b, c
}
xxxxxxxxxx
Create a loop that runs through each item in the fruits array.
var fruits = ['Apple', 'Banana', 'Orange']
for (x of fruits){
console.log(x)
}
xxxxxxxxxx
let fruits = ["apple", "pear", "plum", "orange", "cherry"];
for(var i in fruits)
{
console.log(fruits[i]);
}
xxxxxxxxxx
let arr = ['el1', 'el2', 'el3'];
arr.addedProp = 'arrProp';
// elKey are the property keys
for (let elKey in arr) {
console.log(elKey);
}
// elValue are the property values
for (let elValue of arr) {
console.log(elValue)
}