js for...in array code example
Example 1: javascript loop through array
const myArray = ['foo', 'bar'];
myArray.forEach(x => console.log(x));
for(let i = 0; i < myArray.length; i++) {
console.log(myArray[i]);
}
Example 2: use these instead of a for loop javascript
const array = [1, 2, 3];array.forEach(function(elem, index, array) { array[index] = elem * 2;});console.log(array);
Example 3: for in and for of in js
const iterable = [10, 20, 30];
for (const value of iterable) {
console.log("fef"+value);
}
Example 4: mdn for..of
function* fibonacci() {
let [prev, curr] = [0, 1];
while (true) {
[prev, curr] = [curr, prev + curr];
yield curr;
}
}
for (const n of fibonacci()) {
console.log(n);
if (n >= 1000) {
break;
}
}