I know “break” is used to stop a loop before the satisfaction of a particular condition but I am really confused WHY use break when we have the option to make our criteria better. For example look at this piece of code:
var i = 0;
for (i = 0; i <= 10; i++) {
if (i == 3) {
break;
}
document.write("The number is " + i);
document.write("<br />");
}
I know it very well that this break command will terminate the loop as the value of the variable reaches to 3 but my question is why we use break instead of making our condition like i<=3.
Can someone make it clear with some practical example?
A simple example would be where you are looping through objects in an array, looking for one that satisfies a certain condition. When you’ve found that object, you don’t need to check the rest so use
break.