2

I didn't know this was possible (is it?)

The below code apparently logs values 1 to 5, then breaks out of the 'for' loop, because the 'false' value is returned.

function x() {
    for (var i = 0; i < 10; i++) {
        console.log(i);
        if (i == 5) return false;
    }
    return true
}

console.log(x());

My question is:

  • How come the for loop short-circuits when 'false' is returned? I looked at MDN but there is nothing there about using 'false' to break out of the for loop. Also tried looking at ECMA specs, but sadly too noob.

  • Why doesn't the function return 'true' to the console, as the 'return true' statement exists after the 'for' loop is executed? Even if false somehow returns 'first', shouldn't 'true' return last or also?

2
  • 6
    The return statement will force to return the control from the current function to which called the current function... so in this case it will come out of the loop and return false to the caller Commented Oct 5, 2015 at 6:38
  • If i could give reputation or points or whatever it's called to everyone who answered, I so would. That's cleared everything up for me, thank you guys! Commented Oct 5, 2015 at 7:00

5 Answers 5

9

return false is not breaking your loop but returning control outside back.

function x() {
    for (var i = 0; i < 10; i++) {
        console.log(i);
        if (i == 5) return false;
    }
    return true
}

console.log(x())

Output:

0
1
2
3
4
5
false //here returning false and control also 

Where break will break your loop instead of coming out from function.

function x() {
    for (var i = 0; i < 10; i++) {
        console.log(i);
        if (i == 5) break;
    }
    return true
}

console.log(x())

Will output:

0
1
2
3
4
5 //after this loop is breaking and ouputing true
true 
Sign up to request clarification or add additional context in comments.

Comments

2

The return statement ends the function , so the return true line is unreachable. Hope that helps.

Comments

1

The below code apparently logs values 1 to 5, then breaks out of the 'for' loop, because the 'false' value is returned.

Wrong, it breaks out of the for loop because of the return, not because of the false. You could write return x with the same effect. Point is, return immediatly drops out of the enclosing function, whatever loop or conditional is being executed currently.

Comments

1

The loop will never be executed till 10. return false will return control to caller of the function x as soon as i is equal to 5. The return true line is unreachable.
This is your code

function x() {
    for (var i = 0; i < 10; i++) {
        console.log(i);
        if (i == 5) return false;
    }
    return true
}

console.log(x());

Output:-

 0
 1
 2
 3
 4
 5
 false

If you want it reach to the return true statement you may want to do something like adding a break instead of return false in for loop.

for (var i = 0; i < 10; i++) {
            console.log(i);
            if (i == 5) break;//this will put it outside for loop
        }

Comments

1

If you do not want mess with return or break statements, you can add just one boolean flag. In my example i use isFound flag to run loop until your requirements are meet or end of array.

const arr = [1,2,3,4,5];

for(let i = 0, isFound; !isFound; i++){
  if(i >= 3 || i >= arr.length){isFound = true;}
  else { console.log(arr[i]);}
}

So the output will be:

1
2 
3 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.