Timeline for How to break from nested loops in JavaScript?
Current License: CC BY-SA 3.0
16 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Jan 1, 2023 at 0:49 | comment | added | DexieTheSheep | Yep, this is my go-to (pun not intended) method for breaking out of multiple loops, until I found out about loop labels today from Rust n found out JS has 'em too from this thread. | |
| Jun 7, 2022 at 17:39 | comment | added | Jay Dadhania | @Timo Labelled loops would be the best choice IMO, or you can use a flag variable. | |
| Jun 6, 2022 at 16:21 | comment | added | Timo | @JayDadhania What can be used to break from nested loops in ECMA6 - for .. in and for ... of? | |
| May 2, 2020 at 10:46 | comment | added | fabspro | Are you guys really arguing about whether checking a single boolean value 10000 times is fast or slow? try 100 million times /sigh | |
| Nov 10, 2019 at 4:38 | comment | added | Jay Dadhania | +1 for a novel approach! However, this won't help with for(var a in b){...} or for(var a of b){...} style for loops. | |
| Dec 9, 2018 at 1:30 | comment | added | tim-montague | Clean and declarative approach | |
| Apr 25, 2018 at 14:55 | comment | added | Kim | Another nice language-agnostic approach, although less nuanced than the one above that allows breaking to any of the nested loops. | |
| Dec 11, 2017 at 17:08 | comment | added | Bernardo Dal Corno | I like this but I think in a general sense you would make lots of unnecessary processing - that is, for each iteration of every iterator evalueate abort and the expression. In simple scenarios that might be fine, but for huge loops with gazillion iterations that could be a problem | |
| Sep 20, 2016 at 15:44 | review | Suggested edits | |||
| Sep 20, 2016 at 16:38 | |||||
| Sep 20, 2016 at 15:43 | comment | added | xer21 | Optimization would be to add a break; after setting abort = true; and removing !abort condition check from the final loop. | |
| Dec 2, 2013 at 20:13 | comment | added | zord | No, it's exiting from both loops. Here is the demonstrating fiddle. No matter what condition you set, it's exiting after it's met. | |
| Dec 2, 2013 at 14:53 | comment | added | Robusto | If the condition evaluates to true on the first iteration of the nested loop, you still run through the rest of the 10 iterations, checking the abort value each time. This is not a performance problem for 10 iterations, but it would be with, say, 10,000. | |
| Nov 18, 2013 at 20:08 | history | edited | zord | CC BY-SA 3.0 | deleted 2 characters in body |
| S Nov 1, 2013 at 19:50 | review | Late answers | |||
| Nov 1, 2013 at 19:52 | |||||
| S Nov 1, 2013 at 19:50 | review | First posts | |||
| Nov 1, 2013 at 19:52 | |||||
| Nov 1, 2013 at 19:33 | history | answered | zord | CC BY-SA 3.0 |