Use Conditional Logic with If Statements

I’m going to have a hard time explaining this (or asking this question) but I am not understanding something very fundamental about this excercise. I can solve it so I won’t use the example but one I made up myself:

function gatoradeTest(wasThatGatorade) {
  if (wasThatGatorade) {
    return "Gatorade is delicious";
  }
  return "Gatorade tastes bad";
}

gatoradeTest();

if I pass in True it returns “Gatorade is delicious” and if I pass in False it returns “Gatorade tastes bad.” What’s the deal here? If I pass in true does it just automatically execute the first block of code and if I pass in false it automatically returns the second. Is true automatically assigned to the first part of the code and false to the second? How does Javascript know what’s true or false? This sounds like such a dumb question and I need someone with way more knowledge than I have to help me parse this out.

Here’s another one did:

function nothingIsTrue(trueIsFalse) {
  if (trueIsFalse) {
    return "False";
  }
  return "True";
}

nothingIsTrue(false);

if I pass True it prints False.
Swear to God I’m not trolling. This is driving me batty for 3 hours now.

The literal name of the variable is just so it makes sense to you. JavaScript doesn’t care what you name the variable, only what value you store in it.

I’ve edited your code for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

The code in {...} after an if statement only runs when whats in the parentheses after the if evaluates to true. So if(this evaluates to true) {run this code}. And if you are giving the argument of true to that function the parameter wasThatGatorade holding true will most certainly evaluate to true in that if statement. So it then runs the code in {...} which is a return with a string. A return will also stop a function. So when you give it true it will go to the code in the if then return a string and stop that function, never getting to that second return after the if. If that code in the if statement did not have a return, it would keep running that function and eventually hit your second return, giving you your second string back instead of the one in your if.

But if you give it a false, its going to check it in the if, it will evaluate to false, ignore the code in {...} after the if, and then it will hit the second return with your second string.

Watch the code run.

1 Like

The if statement only executes if its specified condition is true. The syntax is if (condition === True) { executed code if condition is True }. When you pass in True the specified condition becomes (wasThatGatorade === True), which is like writing True === True, and it evaluates to True. Since the condition evaluates to True the block of code return "Gatorade is delicious"; is executed.

If you pass in False the specified condition becomes (wasThatGatorade === True) which is like writing False === True and it evaluates to False. Because the specified condition does not evaluate to True the block of code return "Gatorade is delicious"; is not executed.

The block of code being executed in your if statement uses return and return stops the execution of the function. When you pass in True you will hit the first return statement return "Gatorade is delicious"; and the code is stopped so you can’t hit the second return statement return "Gatorade tastes bad";. When you pass in False the if statement is ignored because the specified condition doesn’t evaluate to True and only the second return statement is executed.

1 Like

ok, backticks! got it! thanks! :slight_smile:

Thank you for the thoughtful answers everyone. I am going to keep chewing on this. Thank you for sharing your time! :slight_smile:

If you pass the variable itself without any test to prove it true or false, it will always evaluate to true, consider this:

let str = “Hello”;
if(str) {
console.log(“whatever”);
} else {console.log(“bye”);
}

since I am never evaluating whether the value of my variable is or not “Hello”, my condition will always evaluate to True.

That is why yours is printing false, because your condition is always evaluating to true and according to that code block if it evaluates to true it has to print false.

Hope this helps.

You are right, I’ve never evaluated 0.
Learned something today.

The first 2 paragraphs is the answer to the question I was asking. Thank you for clearing it up with that fantastic explanation. :smiley: Thank you spanishiwa

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