Basic JavaScript - Use Conditional Logic with If Statements

Tell us what’s happening:

Hi friends at FCC!

I have a question about this if block. It is not a problem with the solution, but I am trying to understand how the syntax works. The if block below contains one if block with a return statement and below it there is a return statement by itself.

When I came to the later lessons it introduced the else keyword, which will execute some code when the if is not true.

But in this example there is no else used, so it looks to me if the if condition is true, it will run the code in the if block AND ALSO run the code at the bottom, because there is no else there. So at first will it set the return value to “Yes, that was true”, and then overwrite it to “No, that was false”?

Please let me know if I have misunderstood this. :blush:

Your code so far

function trueOrFalse(wasThatTrue) {
  // Only change code below this line
  if (wasThatTrue) {
    return "Yes, that was true";
  }
  return "No, that was false";
  // Only change code above this line

}

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.2.1 Safari/605.1.15

Challenge Information:

Basic JavaScript - Use Conditional Logic with If Statements
http://localhost:8000/learn/javascript-algorithms-and-data-structures/basic-javascript/use-conditional-logic-with-if-statements

No, if the condition for the if statement is true, then the code inside the if statement will run and the return keyword used inside there will exit the function completely.

Another way to see what is happening is to add console.log statements and see what is printed to the console

function trueOrFalse(wasThatTrue) {
  // Only change code below this line
  if (wasThatTrue) {
    console.log("I will show up when the if statement is true")
    return "Yes, that was true";
  }
  console.log("I show up when the if statement is false ")
  return "No, that was false";
  // Only change code above this line

}

trueOrFalse(true)
2 Likes

Thank you so much, now I understand the effect of the return statement.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.