Timeline for How to break from nested loops in JavaScript?
Current License: CC BY-SA 4.0
38 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Dec 9, 2023 at 10:47 | comment | added | Lqueryvg | @SamuelKatz because 3 years isn't as long as you think ;-) | |
| Mar 24, 2022 at 22:33 | comment | added | Gary | Ha. I have not come across this - or needed it - in 25 years of JS experience! There's always something to learn in the JS world :) | |
| Jan 11, 2021 at 9:18 | comment | added | Jay Dadhania | I agree that such a bug could be introduced, but code readability and understandability is as much important as re-factoring. And it sure does make the code more readable than flag variables or temp functions. | |
| Jan 11, 2021 at 8:31 | comment | added | Ekrem Dinçel | @JayDadhania 1) I have to find and count for loops every time I see break x. 2) If I copy some parts of the inner loop and paste it to another while restructuring my code, there is a probablity that parser will not raise an error but I will have an logical bug. And if I will not get an error from the parser, I can forgot fixing the break numbers. | |
| Jan 11, 2021 at 7:11 | comment | added | Jay Dadhania | @EkremDinçel I am not sure why a simple statement like break 2; would make the code unreadable or hard to re-structure. | |
| Jan 10, 2021 at 18:33 | comment | added | Ekrem Dinçel | @JayDadhania Now this would make the code unreadable and hard to re-structure. | |
| S Dec 28, 2020 at 4:29 | history | edited | ephemient | CC BY-SA 4.0 | fix broken link to reflect current MDN Docs |
| S Dec 28, 2020 at 4:29 | history | suggested | CommunityBot | CC BY-SA 4.0 | fix broken link to reflect current MDN Docs |
| Dec 28, 2020 at 4:03 | review | Suggested edits | |||
| S Dec 28, 2020 at 4:29 | |||||
| Jul 20, 2020 at 14:47 | comment | added | qräbnö | Does this work also for underscore _.each or _.some? | |
| Jul 14, 2020 at 18:18 | comment | added | ecc521 | How have I never seen this before... Perfect. In the age of ES6, I've never even ran into a case where this made sense to do for literally years. That supercontinue is huge when filtering input. | |
| Jun 17, 2020 at 14:27 | comment | added | Anders | OMG! This is like stepping back to 1995 and Visual Basic 5! | |
| Apr 6, 2020 at 19:35 | comment | added | Jay Dadhania | If only JS could automatically label the loops as ..., 3, 2, 1 (with 1 being the innermost) - then we could use break 2; as we are used to in other languages. | |
| Jul 24, 2019 at 2:38 | comment | added | Dave Newton | Not to mention that some problems are much cleaner with judicious label usage. Of course there are other ways to do it--that doesn't mean those other ways are better, cleaner, easier to understand, etc. | |
| S Jan 4, 2018 at 21:19 | history | suggested | user2200891 | CC BY-SA 3.0 | That domain expired |
| Jan 4, 2018 at 19:26 | review | Suggested edits | |||
| S Jan 4, 2018 at 21:19 | |||||
| Jan 16, 2017 at 20:01 | comment | added | The Dembinski | @JérémyPouyet - Your logic for down voting is inane and unwarranted. It answer the OP's question perfectly. The question is not concerned with your opinions regarding legibility. Please reconsider your approach to assisting the community. | |
| Oct 26, 2016 at 10:15 | comment | added | Jérémy Pouyet | Downvote... There is so many ways to do this without using labels. ex: use another function and return or replace your for..in by another loop with an additional condition. If labels aren't used, it's not because it's not known, but because code quickly become unreadable! | |
| Jan 12, 2016 at 16:54 | review | Suggested edits | |||
| Jan 12, 2016 at 17:21 | |||||
| Nov 12, 2015 at 21:17 | comment | added | Gary Willoughby | @SeantheBean Done. This does seem like the more straightforward answer and not open to abuse because it's only available to continue and break. | |
| Nov 12, 2015 at 21:15 | vote | accept | Gary Willoughby | ||
| Nov 9, 2015 at 20:38 | comment | added | Sean the Bean | @GaryWilloughby Please consider making this the accepted answer. If I understand you correctly, your objection is that you "absolutely hate GOTO's and consider them bad practice", which is a highly subjective reason, and not very relevant since labels are fundamentally different from gotos. The number of upvotes for this answer is sufficient evidence that this is the answer most people reading this question are looking for. (And if I were a betting man, I'd also bet that means the majority of people don't consider using labels to be bad practice.) | |
| Nov 9, 2015 at 20:22 | comment | added | Sean the Bean | @Web_Designer I believe your comment is out of date. Nowhere in the MDN docs does it say to "avoid using labels". Please consider revising or removing your comment. | |
| Oct 5, 2015 at 7:02 | comment | added | kellanburket | Opinions about supposed 'best practices' aside, if I open up another programmer's code and they're breaking out of a nested loop--this is the answer I want to see. That is what I consider readable: How long does it take me to digest someone's code and make sense of it? Plus as many others have pointed out, it's performant. Give me this answer over any of the other more "correct"/convoluted solutions any day. | |
| Sep 19, 2015 at 20:30 | comment | added | a7omiton | I think this keeps the logic in one place, hence easier to debug. But that's coming from a JS novice | |
| Aug 18, 2015 at 21:53 | comment | added | Josh Hibschman | Downvoting for a highly upvoted bad practice (i.e. this is go-to like programming in JS). Recommend reading JavaScript The Good Parts, and updating your answer. | |
| Jul 1, 2015 at 11:54 | comment | added | Florian Leitgeb | Wrapping in a function in my opinion is way less readable then labeling and breaking out of loops this way, because most people are used to similar methods in different programming languages. I am now coding for about 3 years C#, JavaScript, Java and I find this approach way more readable than the answer from harley.333 down here. | |
| Mar 2, 2015 at 3:05 | comment | added | kvanbere | I don't think wrapping in a function creates more maintainable code versus just a few labels and breaks IMHO. What happens when you have multiply nested loops 3 or 4 levels deep -- 3 or 4 separate spaghetti functions? | |
| Nov 4, 2014 at 16:00 | comment | added | Qix - MONICA WAS MISTREATED | @seanmakesgames And functions cause considerable overhead. It depends on how they're used. | |
| Sep 25, 2014 at 20:45 | comment | added | seanmakesgames | This is bad practice. Consider answer with wrapping loop in a function strongly. Creates more readable and maintainable code. | |
| Aug 8, 2014 at 21:12 | comment | added | Ivan Black | It is possible to use loop: { /* loop here */ } for readability. | |
| May 2, 2014 at 20:43 | comment | added | XML | MDN says "avoid using labels" purely on readability grounds. Why is it not 'readable'? Because nobody uses them, of course. But why don't they use them? ... | |
| Jul 21, 2012 at 18:22 | history | edited | Celmaun | CC BY-SA 3.0 | added links to documentation |
| Jul 21, 2012 at 18:17 | comment | added | Celmaun | WTF why haven't I seen this being used somewhere in my 3 years with JavaScript :/.. | |
| May 31, 2012 at 21:50 | comment | added | devios1 | @NielsBom You are correct: it breaks out of the loop with the given label. It doesn't goto the label. You are essentially naming a loop and saying I want to break out of suchandsuch a loop. | |
| Dec 8, 2011 at 15:17 | comment | added | Niels Bom | Is it "breaking to a label"? It looks more like you're naming a block with n optionally-nested loops in it and then abort the "walking through the block" by calling break {label}. | |
| Jul 16, 2009 at 15:45 | history | edited | ephemient | CC BY-SA 2.5 | added 165 characters in body |
| Oct 8, 2008 at 14:53 | history | answered | ephemient | CC BY-SA 2.5 |