Skip to main content
1 of 2
Vural
  • 8.8k
  • 11
  • 43
  • 58

I know its quite an old question but I didn't see any answer, which clarify that why and when its possible to break with return.

I would like to explain it with 2 simple examples:

1. Example: In this case, we have a simple iteration and we want to break with return true, if we can find the three.

function canFindThree() { for(var i = 0; i < 5; i++) { if(i === 3) { return true; } } } 

if we call this function, it will simply return the true.

2. Example In this case, we want to iterate with jquery's each function, which takes anonymous function as parameter.

function canFindThree() { var result = false; $.each([1, 2, 3, 4, 5], function(key, value) { if(value === 3) { result = true; return false; //This will only exit the anonymous function and stop the iteration immediatelly. } }); return result; //This will exit the function with return true; } 
Vural
  • 8.8k
  • 11
  • 43
  • 58