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

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

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?

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

i have a question on the if…else statement …why we didn’t put a semi-colon after the code block of else statement for example:

if (sale) {
console.log(‘Time to buy!’);
} else {
console.log(‘Time to wait for a sale.’)
}
*I meant after the last parentheses of else …
why didn’t we write it as
else {
console.log(‘Time to wait for a sale’);
}

Semi-colon tells the interpreter to stop parsing and begin compiling. An if statement does not end untl after the else block. Even then we don’t give it a semi-colon since it is well delimited and the interpreter knows to parse from if to the final }.

The statements inside the blocks may well need a semi-colon, though we might be able to relax that requirement when there is but one statement in the block. Newer iterations of JS in most browsers will auto-insert them but we should not depend upon this a reliable feature.

1 Like

Not quite understanding why wouldn’t write
If (sale === true)
instead of
if (sale)
at the beginning.

It doesn’t seem to make sense. Can anyone explain the logic there?

I let sale = true;

sale = false;

if(sale) {
console.log(‘Time to buy!’);
} else {
console.log (‘Time to wait for a sale.’)
}

1 Like

That would be,

if (sale === true)

Note the comparison operator. = by itself is assignment.

The conditional argument of an if statement always yields a boolean so direct comparison to a boolean is redundant. We only need to test for truthiness

if (sale)

does just that.

2 Likes

Thanks @mtf . Perfectly explained. Many thanks.

1 Like

You’re welcome!

Eventually we all get a handle on truthy and its counterpart, falsy. This is a valuable tool to have in our kit.