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.

I had a question regarding the semantics of spaces within JavaScript code:

On the Learning (left) side, part of the code is written as if (false) { but on the JS exercise (right) side, it doesn’t have a space between the words “if” and “(false)”. Which should it be or does it matter?

Thank you in advance!

The SCT (Submission Correctness Test) might not be written to accept the space, but JS doesn’t care. White space is largely ignored by the parsing engine.

Choose a style, after reviewing a couple of style guides, then stick with it so your work is easy to read as a body of code.

1 Like

Curious about how the computer reads if/else statements, would appreciate any insight on the matter.

I know myself from just writing it out as so that you don’t need else between the curly brackets like } else {, and can instead write it like
end of if
}
else {

}

However i’m curious, does the computer read an if statement, and then for the rest of your code look for an else, unless it hits another if? Or does it have to come directly after the if’s closing curly bracket?

We can write an if statement with or without added white space. JS ignores most of it as long as the expected token or value is present as it parses the statement. Write it all on one line, if you like (though your readers might frown on that).

Where we write the LBRACE and RBRACE is a matter for us to decide, but once decided, stick with that presentation throughout. There are different schools of thought, but none is more right than the other given any of them will work, the proviso above notwithstanding.

My own style uses,

if (...) {
  // code
} else if (...) {
  // code
} else {
  // default
}

Being script, nothing is compiled until it is run. JS may not even see the else if or the else clauses during run time if the opening condition is truthy.

Gotcha, thanks a bunch! One line writers scare me so no worries there :sweat_smile:

1 Like

the else statement is just the opposite of the if statement

is just to say,
let sale = 5; //the initial value
sale = 3; // oops, i changed my mind, re-assign new value to sales.

Hello there
Why is that if statements in many programming languages including javaScript does not fallow the math logic namely the Discrete Math logical statements? I mean it only runs if the condition is true? but it does not do anything in the result(q) block when the assumption is false? According to logical statements if the condition (p) is false then the whole statement is true irrespective of what the result will be.
if p => q
if p then q

p q p => q
F F T
F T T
T F F
T T T

The answer is simple: Control flow. Discrete Math is abstract. An if statement is imperative.

Abstracts tend to hide a lot of details the mechanics of which are mathematical principles, axioms, theories, etc. which include set theory, analytical geometry, linear algebra, calculus and limits, and so on.

Imperative statements require actions to follow conditional expressions. Control flow must be directed down one path or another to find and execute the next statement. The computer, or language does not possess the power of reasoning. Our programs, at the hand of the programmer must follow prescribed paths from statement to statement without the aid of a human (during runtime).

1 Like

What im understanding in (if else) is that it changes the condidtion of the statement inside the curly braces?
let age = 18;

if (age >= 18) {
console.log(“You are eligible to vote.”);
} else {
console.log(“You are not eligible to vote yet.”);
}
let we are declaring a variable = 18
if we are saying the age equal to or greater than 18
but if i add 20 years old in the if it will change the condition to else

just a question?