Learn Create Your Own Split Screen

Next Page

Another use for break

As well as being of central importance in switch-statements, break can also be useful inside for-loops and while-loops.

It's a way to exit the loop early.

for (var i=0; i < aliens.length; i++) { aliens[i].do_something(); if (game_over) break; }
In the example above, the for-loop will do something for each alien in the alien array, but if a boolean variable called game_over becomes true, the for-loop will exit and the remainder of the aliens will not be processed.