If I have the following for loop
for (var i = 0; i < SomeArrayOfObject.length; i++) {
if (SomeArray[i].SomeValue === SomeCondition) {
var SomeVar = SomeArray[i].SomeProperty;
return SomeVar;
}
}
Does the return statement stop the function’s execution?
Yes, functions always end whenever their control flow meets a
returnstatement.The following example demonstrates how
returnstatements end a function’s execution.Notes: See this other answer about the special case of
try–catch–finallyand this answer about how theforEachcallback has its own function scope, so it will not break out of the containing function.