0

I'm learning JS and I came across a challenge and I'm curious regarding why the "break;" is needed for this function to work. I though it was only needed in Switch loops. Thanks in advance!

 _.some = function (collection, predicate, context) {

  let result = false;
  for (let i in collection) {
    if (context) {
      if (predicate.call(context, collection[i], i, collection)) {
        result = true;
        break;
      }
    }
    if (collection.hasOwnProperty(i)) {
      if (predicate(collection[i], i, collection)) {
        result = true;
        break;
      } else result = false;
    }
  }
  return result;

};
ilvytex
  • 13
  • 2
  • 1
    "*…in Switch loops*" - a `switch` statement is not a loop? – Bergi May 21 '20 at 18:15
  • I'm not sure what a "switch loop" is, but it's "necessary" so the loop stops. You could also just `return true` immediately, remove the `else`, and `return false` at the end. – Dave Newton May 21 '20 at 18:16
  • Reading up about break statements here may help https://www.w3schools.com/js/js_break.asp – Adam McClenaghan May 21 '20 at 18:16
  • Do you known what the [`break` statement](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/break) *does* in a loop? – Bergi May 21 '20 at 18:16
  • Break exits the for loop. – Markus Zeller May 21 '20 at 18:16
  • 1
    If you've just found the thing you're looking for, you don't need to keep looking for it. – jarmod May 21 '20 at 18:16
  • 1
    @jarmod That's why everything is always in the last place you look ;) – Dave Newton May 21 '20 at 18:17
  • @AdamMcClenaghan or better: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/break – mplungjan May 21 '20 at 18:18
  • @AdamMcClenaghan Please [don't link to w3schools](https://meta.stackoverflow.com/questions/280478/why-not-w3schools-com). –  May 21 '20 at 18:18
  • @ChrisG My bad, I wasn't aware of the issues folks have had in the past with that site, now I do! https://meta.stackoverflow.com/questions/280478/why-not-w3schools-com – Adam McClenaghan May 21 '20 at 18:24
  • In fairness, W3 isn't not the giant cluster of fscks it used to be... it used to be *terrible*. MDN still > W3, though, for a variety of reasons. – Dave Newton May 21 '20 at 18:27
  • @DaveNewton True, but they don't deserve backlinks from SO either :) –  May 21 '20 at 18:40

2 Answers2

0

It's used to speed up the loop. What it does is stop (break out of) the loop. In your code there's no reason to continue looping through the collection, because you found what you're looking for.

  • The `else result = false` is a good reason why it is necessary – Bergi May 21 '20 at 18:18
  • @Bergi Is it? It's initialized to `false`, if it's ever set to `true` the loop exits, isn't the `result = false` superfluous? I might be mis-thinking, though. – Dave Newton May 21 '20 at 18:20
  • @DaveNewton Yes, one probably should drop that statement. My comment was meant to challenge that the `break` statement just speeds up the loop, which sounds like the code would work the same if the `break` was omitted. – Bergi May 21 '20 at 18:25
  • @Bergi Gotcha; makes sense. – Dave Newton May 21 '20 at 18:26
0

Breaking from loops and early returns are both coding patterns (techniques) to speed up a looped code block.

If you are in a loop, and you want to terminate the loop before the loop has completed, you can use "break" to break out and continue with code that follows the loop. This is for instance applied when your loop intends to find the first occurrence of something.

If you are in a function, and there is nothing else to do after the loop, you can alternatively "return" out of the function, from within the loop.

Neither are required. You can also write some if...else code within the loop and obtain the same result. But it is usually not prettier and it can take longer to execute.

Early breaks/returns usually lead to code that is easier to read & understand, and it can execute faster.

Here is a good discussion:

Is it a bad practice to use break in a for loop?

As for switch/case: in most languages you want to "break" out of a case in a switch/case conditional code block, because otherwise the code in all subsequent cases, following the valid case, is also executed. (This is admittedly a confusing "feature" but it is a remnant of how early switch/case constructs were added to languages.)

P2000
  • 1,042
  • 8
  • 14
  • 1
    It's also a "remnant" of sometimes-that's-what-you-want, e.g., Duff's Device. Deliberate fall-through is not uncommon. – Dave Newton May 21 '20 at 18:48
  • Yes agreed @DaveNewton . Perhaps not uncommon, but I have never needed one (maybe because I just dislike falling through). In Swift (a newer language) there is a keyword "fallthrough", because otherwise the case breaks by default. Maybe it's a sign of the times. – P2000 May 21 '20 at 19:09
  • *shrug* There are plenty of cases where falling through has value (otherwise Swift wouldn't add a special token for it); not uncommon at all in C/C++ embedded work. – Dave Newton May 21 '20 at 20:02
  • Languages that allow compound cases don't need an implied fall through, and instead default to break. It's more intuitive to read, and less likely to confuse. – P2000 May 21 '20 at 23:33
  • Falling through has more uses than compound cases, e.g., Duff's Device. There are other reasons fort taking through as well. – Dave Newton May 22 '20 at 00:30