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 179 views
3 answers 232 views
2 answers 180 views
1 answer 166 views
4 answers 380 views
2 answers 194 views
3 answers 134 views
...