How to loop through an array in JavaScript

3 Answers

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

for (let i = 0; i < arr.length; i++) {
  console.log(arr[i]);
}
  
    
      
/*
run:
      
1
2
3
4
5
6
7
      
*/

 



answered Jan 31, 2021 by avibootz
0 votes
const arr = [1, 2, 3, 4, 5, 6, 7];

let i = 0;
while (i < arr.length) {
  console.log(arr[i]);
  i++;
}
  
    
      
/*
run:
      
1
2
3
4
5
6
7
      
*/

 



answered Jan 31, 2021 by avibootz
0 votes
const arr = [1, 2, 3, 4, 5, 6, 7];

for (let i in arr) {
  console.log(arr[i]);
}


  
    
      
/*
run:
      
1
2
3
4
5
6
7
      
*/

 



answered Jan 31, 2021 by avibootz

Related questions

2 answers 168 views
3 answers 217 views
2 answers 169 views
1 answer 155 views
4 answers 338 views
2 answers 181 views
3 answers 110 views
...