How can i stop for/loop if met condition

Hello Everyone! I have list array , I want to display it by Line, Line A1 on the left, line A2 is shown on the right and when the “box_pos” of A1 and A2> 4, I want to show 4 items A1 on the left and 4 items A2 on the right , if “box_pos” A1> 4 and “box_pos” A2 <4 I want to display 5 elements of A1 on the left and 3 elements of A2 on the right.
Here my link to codepen this my link

just use a break keyword when the condition is met like we use to do in switch

Example: -

let i = 0;
for(i = 0; i < 10; i++) {
	if(i == 5) {
		break;
	}
}
console.log(i); //value of i will be 5

Hi @kashimi! Thanks You so much! I apologize for the late reply

let i = 0;
for(i=0;i<10;i++){
if (i == 5){
break;
}
}

console.log(i);

Using a break statement on the i counter variable itself is counterintuitive when you’re using a for loop, you can just change the number of iterations to begin with.

What’s much more commonly done is using break when searching for a value in an unsorted array, then it makes sense to break out when the value is found.