There are other ways to break a loop as well, such as using the return keyword within the body of a function for example. There is another keyword that is similar to that of break called the continue keyword that is worth mentioning also as that keyword can be used to skip a body of code and continue a loop without breaking out of it, but just skipping over any additional code that would run otherwise.
The break keyword would typically be used used in the body of a certain conditional statement within the body of a loop as a way to get out of something that would otherwise be an infinite loop. Another use case in the body of a loop would be to just get out of a loop sooner compared to how long it would take for some other condition to happen that will result in an end to the loop. So then the use of break can often help to reduce the volume of work that would otherwise need to be done, increasing the efficiency of code.
So then the break keyword can help avoid having to loop over the full contents of an array for example when looping over the contents of an array that way. If the array is fairly large the break keyword can help reduce the amount of time it takes for the loop to complete which proves to be more efficient compared to other options such as Array forEach that will always loop over the full contents of an array. Also the break keyword could be used to keep some code from running that I would not want to run if a condition is met that warrants a break statement.
The source code in this post in on Github
1.1 - Break out of a while loop before the end
When using a while loop I often like to make use of the trick where I start an index value at the end and then subtract from the index value while also using th result new value as a way to break out of the loop. That is that the number 0 evaluates to false so by starting a number at a value above zero and subtracting by one for each loop the value will become zero at some point which will result in and end to the loop. However I can use this in conjunction with a break statement to also get out before that happens, simply put something like this:
1.2 - Basic switch example
The break keyword is also something that will typical going to need to be used if one is to bother with switch statements. The basic idea with a switch statement is that a value is used with it when starting a switch block, and then in the body of the switch block at least two if not more case statements are used to define what to do for each possible value of the value given to the switch.
For this example I am using a switch in the body of a function that I ten intend to use with the map array prototype method. For each element in a source array the array map method will be called and when dong so the parse element method that I have made that contains a switch will be called which will return a new value for an element in the source array. The end result of calling array map with the parse element method will be a new array with new values based off of values in the source array. In this case I have an array with mixed string and number values, as well as number values that can end up being NaN which I would like to handle differently. When calling array map with the parse element method the final outcome will be an array of just numbers where zero will end up being the number value for certain situations.
1.3 - Using labels and nested loops
I should go over the use of labels in detail in a later section in this post, but for now I will be quickly going over a basic example of this here. Even when it comes to a basic example of labels I guess that the use of labels might not be so basic for some though. The typical use of them would involve one or more nested loops in the body of a loop.
I do not come across switch statements often, and I do my best to avoid using them but never the less when working with a switch statement the break keyword is also used for each case statement of a switch. That is use use the case keyword in the body of a switch, followed by a value, and then terminated the case with a colon. I then do whatever needs to be done in the case block, and then finish with a break.
3 - javaScrpt break and labels
When making something that has two or more nested loops it is possible to use labels as a way to break out of the desired loop within the nested loops. By default the break keyword will break out of the loop from which it is called, but if for some reason I want to break out of a loop that is one other than that one I can use labels as a way to go about doing just that.
Some developers do not recommend the use of break and labels as it is similar to goto. It is then recommended to make a more functional approach with something like this, that would also be a a little more fine grain as well. Still this is one other way to go about using the break statement and can be used in conjunction with labels not just in switch statements but with loops as well.
So there is using break, and then there is finding way to not go about using break.
here I have an example that will break out of the loop in the event that anything other than a number is detected, and will continue if the number is not a power of two. This might not be the best example, but if there was some code that did some heavy lifting after a continue statement, and it was a fairly lengthly array of elements that need to be looped over it might make a difference.
6 - Conclusion