1

I don't understand why the below code doesn't log 1 and 2 and then return false. Doesn't the return break out of the forEach scope and firstLayer's scope to return false? Right not it's not even breaking the forEach scope.

var arrayExample = [1,2,3];

function firstLayer (arr) {
  arr.forEach(function (num) {
    console.log(num);
    if (num === 2) {return false;}
  });
  return true;
}

firstLayer(arrayExample);

//logs 1,2,3 and returns tru

e

akantoword
  • 2,824
  • 8
  • 26
  • 43
  • 5
    Possible duplicate of [How to short circuit Array.forEach like calling break?](http://stackoverflow.com/questions/2641347/how-to-short-circuit-array-foreach-like-calling-break) – Abaddon666 Jul 07 '16 at 22:30

3 Answers3

1

I'm not sure if you're using forEach for any particular reason but a for loop would be my go to if you need to break anything out of the loop. I'm not sure if you can escape a forEach like that, but here's the working code:

var arrayExample = [1,2,3];

function firstLayer (arr) {

 for(i=0; i < arr.length; i++){
    var num = arr[i];
    console.log(num);

    if(num === 2){
        return;
    }
  }
}

firstLayer(arrayExample);

Hope this helps

ConorJohn
  • 637
  • 2
  • 11
  • 24
  • is there a reason why the return works for a 'for' loop but not forEach? or is that just the way it is? – akantoword Jul 08 '16 at 18:02
  • 2
    @jlei The reason is that `for(...)` is a language construct, not a function. Then the `return` exits from its parent function, which is the `firstLayer()` one. At the opposite, `foreach()` _is_ a function, so `return` exits for it. – cFreed Jul 08 '16 at 21:43
1

It's absolutely normal: your return false; is part of the arr.foreach() function body.

So this arr.foreach() function returns false when num = 2, true otherwise. But:

  • this return has no impact on how the function works, since there is no statement after it
  • the returned boolean value is never used in the context of the external firstlayer() function

This external function, at the opposite, always returns true.

cFreed
  • 4,404
  • 1
  • 23
  • 33
  • would it be correct to say that after each iteration in the loop (after each console.log), there is an implicit return anyway? so thus my return false makes no difference? if so, how would i break out of this loop then if I want to stop after 2? – akantoword Jul 08 '16 at 18:01
  • 1
    @jlei "my return false makes no difference?" Absolutely yes. And sorry, I only answered to the "why does it work so?" included in your question text, and not to the "how to make it work as I need?" of the title! This 2nd question is answered by ConnorJohn. And look at my comment under its answer for a response to your own comment. – cFreed Jul 08 '16 at 21:40
0

Try this one. It would iterate through it like a loop. It would break at 2 and won't continue to 3.

var arrayExample = [1,2,3];

function firstLayer (arr) {
  $.each(arr, function (num) {
    console.log(num);
    if (num === 2) {
      return false;
    }
  });
  return true;
}

firstLayer(arrayExample);

This is how they did it here: How to break out of jQuery each Loop

Community
  • 1
  • 1
Vinz
  • 211
  • 1
  • 2
  • 9