How to use for-of loop in JavaScript ES6

2 Answers

0 votes
const arr = [1, 2, 3, 4, 5];

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


     
/*
run:
   
1
2
3
4
5
 
*/

 



answered Mar 8, 2020 by avibootz
0 votes
const arr = [23, 76, 181, 89, 9187];

for (const [index, element] of arr.entries()) {
    console.log(index + '. ' + element);
}

     
/*
run:
   
0. 23
1. 76
2. 181
3. 89
4. 9187
 
*/

 



answered Mar 8, 2020 by avibootz

Related questions

1 answer 172 views
1 answer 162 views
2 answers 246 views
1 answer 161 views
...