This is what I want to do: cycle through a complex data structure and if one of the elements meets some condition, return it. But I don’t think it’s possible to return from within an ‘if’ that’s within my for loop. What’s the best practice? Thanks.
function findIt(x) {
for (i in someDataArray) {
v = someDataArray[i];
if (*v meets some condition*) {
return v;
}
}
}
It’s perfectly legal to issue
returnstatement from within anif.The only issue here is what happens if it’s not found? Here the function will implicitly return
undefined. It may be better to make this explicit. This is a matter of style though.