Why do we write `if(sale)` instead of `if(sale===true)`?

Yes, for sure. This is not funny to waste time typing questions if you found the answer on the forum.

Hello,

I have exactly the same code and it works. That’s mean that the message “Time to wait for a sale” appears in my terminal. So the code seems to be not the problem.

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

Hello everyone. First timer here. I’d like to get some clarification: is ‘sale === true’ the same as ‘sale = true’?
In other words, can the assignment operator ‘=’ be written as ‘===’?

Not at all. The single = is the assignment operator, === is the identity operator (identical relation in type and value).

Thank you for responding.

1 Like

I had the same question, but then I realized the line right above the if statement sets the condition to be either true or false, therefore if (sale) would be either true or false, Change the line right above the if statement, and the output changes.

If you use 1 instead of true or 0 instead of false, the code still works interestingly.

let sale = 1;

sale = 0;

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

That is because 1 is truthy and 0 is falsy. The if statement evaluates truth value, whatever form of expression it takes.

1 Like