0
Q:

for of js

var texts = [
    'a1',
    'a2',
    'a3',
];

for (var i = 0; i < 3; i++) {
    console.log(texts[i]);
}
/* Result
a1
a2
a3
*/
2
var texts = [
    'a1',
    'a2',
    'a3',
];

for (var i = 0; i < texts.length; i++) {
    console.log(texts[i]);
}
/* Result
a1
a2
a3
*/
2
const array = ['hello', 'world', 'of', 'Corona'];

for (const item of array) {
  console.log(item);
}
7
let panier = ['fraise', 'banane', 'poire'];

for (const fruit of panier) {
  // console.log(fruit);
  console.log(panier.indexOf(fruit));
}
3
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"
}
2
const numbers = [1,2,3,4];

for(const item of numbers){
  console.log(item);
}
1
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)
}
0
const array1 = ['a', 'b', 'c'];

for (const element of array1) {
  console.log(element);
}

// expected output: "a"
// expected output: "b"
// expected output: "c"
0
Object.prototype.objCustom = function () {}; 
Array.prototype.arrCustom = function () {};

let iterable = [3, 5, 7];
iterable.foo = "hello";

for (let i in iterable) {
  console.log(i); // logs 0, 1, 2, "foo", "arrCustom", "objCustom"
}

for (let i of iterable) {
  console.log(i); // logs 3, 5, 7
}
0
let iterable = new Map([["a", 1], ["b", 2], ["c", 3]]);

for (let entry of iterable) {
  console.log(entry);
}
// [a, 1]
// [b, 2]
// [c, 3]

for (let [key, value] of iterable) {
  console.log(value);
}
// 1
// 2
// 3
0

New to Communities?

Join the community