<!DOCTYPE html>
<html>
<body>
<p>Click the button to loop through the indices of an array with four elements, but exit the loop when i is equal to "3".</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction() {
var cars = ["BMW", "Volvo", "Saab", "Ford"];
var text = "";
var i;
for (i = 0;; i++) {
if (i == 3) {
break;
}
text += cars[i] + "<br>";
}
document.getElementById("demo").innerHTML = text;
}
</script>
</body>
</html>