How to break from a loop in JavaScript

2 Answers

0 votes
const arr = [ 12, 98, 80, 50, 88, 35, 70, 60, 97, 85, 89 ];

for (let i = 0; i < arr.length; i++) {
   if (i === 4) {     
   		break; 
   }
   console.log(arr[i]);  
}

    
    
/*
run:
    
12
98
80
50
    
*/

 



answered Jun 11, 2021 by avibootz
0 votes
const arr = [ 12, 98, 80, 50, 88, 35, 70, 60, 97, 85, 89 ];

let i = 0;
while (i < arr.length) {
	 if (i === 4) {     
   		break; 
   }
   console.log(arr[i]);  
   i++;
}


    
    
/*
run:
    
12
98
80
50
    
*/

 



answered Jun 11, 2021 by avibootz

Related questions

2 answers 188 views
2 answers 211 views
1 answer 140 views
2 answers 246 views
1 answer 132 views
...