FAQ: Conditional Statements - If...Else Statements

This community-built FAQ covers the “If…Else Statements” exercise from the lesson “Conditional Statements”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Web Development

Introduction To JavaScript

FAQs on the exercise If…Else Statements

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

The lesson says the else statements run if the condition is false. Is this to say that if statement conditions should always be true?

1 Like

There is no rule that specifies that. We use if to test a condition, on the basis we don’t know what its state is.

if condition:
    # this is the branch for a truthy condition
else:
    # the branch for anything not truthy

4 posts were split to a new topic: What is the formatting for if/else statements? (Semicolons and indentation)

6 posts were split to a new topic: Why do we write if(sale) instead of if(sale===true)?

I need help understanding: let sale = true but then the second line has sale = false
?

I am just confused here. We made a let variable called “sale”, with the content of it true, right? But then we changed it to false, how come, if it’s false now, a line it the if function says if(sale), however, i think that means “if false”, but if its false why doesn’t it do the function?


let sale = true;

sale = false;

if(sale) {
  console.log('Time to buy!');
}

I hope i get an answer.

Because false is not truthy, and only true or truthy conditions are allowed into the first branch of an if statement.

1 Like

I understand the basic concept of this, but I am confused as to how the code determines the true or false aspect of it. Here is my code:

let sale = true;

sale = false;

if (sale) {

console.log(‘Time to buy!’);

} else {

console.log(‘Time to wait for a sale.’)

}

Conceptually, if it is “True” then the output will be “Time to buy!”, and if it “False” then the output will be “Time to wait for a sale!” But my question relates to how it knows which is which. Since we set the first variable to True and the second one to False, is that why it connects the first statement with being True and the second one to False? Meaning, is the order of the set variables important?

Only in regards to which value will take hold. The latter declaration replaces the value, so ‘false’ holds.

if (sale)

tests for truthiness. ‘false’ is not truthy.