1

I have a for of loop where I need to exit if there are no resulting values. It occurs to me that I could use an early return statement to handle this, or use a break statement. To be clear, in this case, there is no additional code to execute WITHIN this block of code after the part I'm skipping, so I'm assuming either one would work here (break or return). Any functional or performance reason to use one over the other in this particular case?

OPTION 1: (break)

  for (let diff of differences) {
    if (!diff.path) break;

    if (diff.path[0] !== "updatedAt") {
      const docChange = new ChangedProp(doc, diff, lastEditedBy, "customer");
      docChange.log();
    }
  }

OPTION 2: (return)

  for (let diff of differences) {
    if (!diff.path) return;

    if (diff.path[0] !== "updatedAt") {
      const docChange = new ChangedProp(doc, diff, lastEditedBy, "customer");
      docChange.log();
    }
  }
9
  • I suspect it makes no difference at all in performance, but I would think in terms of how you imagine the function might change over time; one might be better than the other when considered that way. Commented Jun 5, 2019 at 12:54
  • Possible duplicate of Difference between Return and Break statements - while in Java context, the answer applies to Javascript as well. Commented Jun 5, 2019 at 12:55
  • That's a fair point, in which case break would probably be the better choice. Commented Jun 5, 2019 at 12:55
  • 1
    @nbokmans, this is a JavaScript question, not Java. Commented Jun 5, 2019 at 12:56
  • @Ademo that is irrelevant. Just because it is tagged Java doesn't mean it doesn't apply to Javascript (in this situation). Commented Jun 5, 2019 at 12:56

1 Answer 1

7

Any functional or performance reason to use one over the other in this particular case?

No, if we assume that the for-of loop is the last thing in the function containing it. There may be style arguments one way or the other, but no functional or performance reason.

Obviously, if there's code after the for-of loop, using break will result in that code getting run, and using return will result in that code being skipped. That's a significant functional difference:

function a(array) {
  for (const value of array) {
    if (value % 2 == 0) {
      break;
    }
  }
  console.log("This line gets reached");
}
function b(array) {
  for (const value of array) {
    if (value % 2 == 0) {
      return;
    }
  }
  console.log("This line does NOT get reached");
}

const arr = [1, 2, 3];
a(arr);
b(arr);

In that example, the code in a and b are the same other than that a uses break and b uses return (and the text logged at the end is slightly different).

Without that console.log after the loop, though, no functional difference.

Sign up to request clarification or add additional context in comments.

2 Comments

Agreed. And therefore, in terms of being explicit, I think "break" is the better option here, because, even if there isn't additional code to run now, there may be in the future.
@Ademo - Yeah, it's totally a judgement call and often a case-by-case one depending on what the function does. :-)

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.