FAQ: Conditional Statements - The if keyword

3 posts were split to a new topic: Do I need a semicolon after an if statement?

2 posts were split to a new topic: Old lesson checkpoints

A post was merged into an existing topic: What condition is necessary to show that sale is true?

7 posts were split to a new topic: What condition is necessary to show that sale is true?

2 posts were split to a new topic: Why can’t I use let to reassign a variable?

2 posts were split to a new topic: Did I find a bug?

2 posts were split to a new topic: Why didn’t this work?

2 posts were merged into an existing topic: Do I need a semicolon after an if statement?

6 posts were split to a new topic: Why doesn’t my code match?

2 posts were split to a new topic: Why is my code wrong?

Hello there!

When doing the exercise on “If Statements,” I made a mistake at step 2 and typed if (true) initially, rather than if (sale). ‘Time to buy!’ appeared in the console, which didn’t surprise me as let sale = true.

After changing to sale = false, if (false) yields nothing in the console, of course. But if I type, if (true), ‘Time to buy!’ does appear in the console, even though the variable has been changed to false.

Please can someone explain why this is? I would think that if sale = false, if (true) would not yield anything in the console because there isn’t a variable that is true. (Just experimenting to see what happens off the back of my initial mistake!)

EDIT: I asked my friend and they said I created an “infinite loop” by not defining what it is that’s true. So I understand my mistake now on a simple level, but would still appreciate more detailed feedback if anyone wants to give it :slight_smile:

if (true) {

}

is an absolute condition so there is no way to change its state. Such a loop requires a breaking condition within to code body…

if (true) {
    //  some code creates a state

    if (state === null) {
        break
    }
}

null in the above is simply a placeholder for example.

It is late today and I am bit wrecked so thats probably why I read “a” as “o” in the code but having two different fonts for code and description is not helping.

I wondered why the code needs to be separately into different lines? Cant that 1 code exists on a single line?
I originally typed the code in 1 line and the result shows what is expected but didnt pass the exercise. Then i looked at the answer and is because we need to separate the code into 3 different lines.

I am probably making some sort of obvious error here but I cannot figure this out.
I am creating the sale variable and setting it to true.
This works.

Then I am setting the sale variable to false.
This also appears to work.
Finally, I am telling the program to print a message if sale is true.
And, the message prints.
If I look to see the value of the variable, it now says that it is true again…
Why? Why is the variable still seen as true, when I had set it to false most recently?
Screenshot included, and my code,

let sale = true;

console.log('first test' + '=' + sale);

sale = false;

console.log('second test' + '=' + sale);

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

console.log('third test' + '=' + sale);

The condition given above is an assignment, not a comparison. The assignment does take place so the variable is now true. Because the condition didn’t ‘fire’ it was treated as a statement, followed by an anonymous code block that also executed.

if (sale === true)

Note the comparison.

2 Likes

The condition given above is an assignment, not a comparison.

Ahhhhh, so the fact that I had set it to false, then did

if (sale = true)

had set it again to true!
Thanks so much! You are always so helpful!

1 Like

Hello,

Can you help me. I can`t figure out what is error in my code, but platform get the error message.

let sale = true;
sale = false;
if (sale) {console.log(Time to buy!)};

Thanks in advance.

There doesn’t appear to be a syntax error. What does the error message say?

when you create a condition
if (true) {
console.log(‘Time to buy!’);
},

what you are saying is that the code should run as long as the value (true) is true.

That means you can even delete the variable sale and the code will run because “true” is not a variable, it is a value and “true” will always be “true”

Rem: ‘sale’ is a variable holding a value called ‘true’ meaning sale depends on true.

But true is a value on its own and can function on its own