Confused about IF statements

In basic Javascript lessons on IF statements, the following example is used:

function test (myCondition) {
  if (myCondition) {
     return "It was true";
  }
  return "It was false";
}
test(true);  // returns "It was true"
test(false); // returns "It was false"

What is stopping “It is false” from being returned every time? That part of the function is not included as an ELSE, so wouldn’t it be run every time? Is there something that halts the function at the IF statement if the test is TRUE?

Returning a value from a function automatically stops the function from executing any further. Thus, if you return in the if statement then the return following the if statement is never executed.

1 Like

Brilliant. Thanks for the quick reply!