IF Statement Challenge Question

Tell us what’s happening:

I solved the challenge but I don’t fully understand why it works. In the example, we have a variable (wasThatTrue) as the parameter. When the function is executed, wouldn’t it always evaluate to true because the if statement is calling the same variable? I hope that makes sense.

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 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.75 Safari/537.36 Edg/86.0.622.38.

Challenge: Use Conditional Logic with If Statements

Link to the challenge:

I’m not sure I fully understand. But:

if (wasThatTrue) {

Will check if wasThatTrue is “truthy” - in other words, if it is not false, 0, -0, an empty string, null, undefined, or NaN. Those are all falsy - everything else is truthy. Of course, if you are sending a boolean, then it’s just true or false, but that works a well.

So, if that is truthy, it returns “Yes, that was true” and exits the function. If wasThatTrue is false (or technically any of the 7 falsy values) then it will skip over the if statement, returns “No, that was false” and exit the function. (A return always exits the function, immediately.)

I’m not sure if that clears it up. If not, let us know.

So essentially this function just checks to see if the data in the variable is “falsy” and if not it returns “Yes, that was true”.

Yes, I mean more accurately it check if it is truthy, but it’s easier to list the 7 falsy values instead of the practically infinite number of truthy values. If you’re sending it a boolean value, then you can think in terms of true/false - just be aware that under the covers, the if is converting what it gets to a boolean - truthy values become true and falsy values become false - as far as the if is concerned.

I feel like I have a good grasp of IF statements and I think I get how that function works now.

I believe the confusion was due to the curriculum not mentioning truthy or falsy values beforehand.

Right, but part of teaching sometimes is not overloading the student with information. And if you know that trueOrFalse will always be passed a boolean, then you can think of it in terms or true/false. I was just pointing out that technically if wasThatTrue is the number 123, then it would be treated in the if statement as true, because it is “truthy”. I assume they will cover that at some point. I just pointed it out to be thorough in my explanation, hoping that it doesn’t add confusion.

But remember that this is confusing stuff. Programming is hard. That’s why it pays well. That fact that you are asking questions, wanting a deeper understanding is a good thing. But don’t worry if everything doesn’t make perfect sense right away. That is fine. That is normal. It will gradually become clearer and clearer. No one understands all of this perfectly the first time it is presented. Just keep moving forward.

3 Likes

Thank you for the further explanation. I feel as if I have a better understanding now and I will keep pushing forward. It’s just hard for me to move on when there is something that I don’t understand because I want to make sure I have a solid knowledge base before I move on to more complicated subjects.

1 Like

I know the desire to wait until you understand it fully - that’s a healthy impulse. But I also think that can be paralyzing. This stuff is too confusing to get it perfect before moving one. Just get a “kinda” understanding for now and know that you’re understanding will deepen as you use it.

1 Like