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

i see! yes i just learned that…

a string can be a falsy. but is limited to only falsy values. i get it now! i should of went to the next exercise before commenting. i got excited lol.

1 Like

Must confess, got PHP mixed in with this. '0' will be truthy since it is a string object with length 1.

+('0')
Number('0')

will both be falsy because they cast the string back to a number.

1 Like

The better question is, why do they teach it this way? They try to explain and don’t. Please refresh your lessons, Codecademy.

Hey just though i would throw my two cents in about if statements conditions.

No matter what you throw into an if’s condition block (Eg. if (myAge < 30) {/* run some code bob */}) it will be looked at as a true/false, you can also treat it working similarly to method call (be warned you will get some dev panties in a twist if you start calling an if statement a method :joy:) if it makes more sense, you pass in your condition as an argument and the if returns a true or false boolean based on the conditions “truth”, in this example if my age is under 30 it shall return true and the following code block will be executed.

To give a bit more of a “code example”:

let myAge = 29

if (myAge < 30) { // myAge = 29 so condition will return true
   myAge++; // adding 1 to myAge to make value 30
}

if (myAge < 30) { // myAge = 30 so condition will return false
   /*
   run code for weirdly arbitrary age gate (this will never be reached in this code)
   */
}

But what if instead the first if statement had the same condition, but then instead of incrementing the myAge variable, we create a boolean to let us know if our age gate can be passed, changing the second statement to take the new variable as its condition:

let myAge = 29

//declaring the boolean as true, will only be modified if myAge is under 30
let ageOverThirty = true;

if (myAge < 30) { // myAge = 29 so condition will return true
   ageOverThirty = false; // setting boolean value to false
}

if (ageOverThirty) { // ageOverThirty = false so condition will return false
   /*
   run code for weirdly arbitrary age gate (this will never be reached in this code)
   */
}

In the second if statement, we are passing in ageOverThirty with the value false as the condition, breaking it down further we are essentially just passing false into the condition block, the variable name is regardless to the outcome of the operation as we are only interested in the value at the moment (obviously in real working code you would want to use the right variable!), so we can also look at it like this:

if (false) { // hard-coded boolean expression (not recommended!)
   /*
   run code for weirdly arbitrary age gate (this will never be reached in this code)
   */
}

So if we look at what is going on, the if gets declared, the condition is hard-coded to be false, the if checks the condition which is simply a false boolean value, the if reads the only possible outcome of the condition as false, therefore the codeblock will not execute.

Writing if (ageOverThirty === true) {/* run more code bob */} will give you the same outcome but is unnecessary as you are writing more code for the same outcome, the most important thing to remember while coding is to always look for the simplest solution to your problem, unnecessarily complex code will always come back to pin the tail on the donkey!

1 Like

This is an amazing explanation! Thank you!!

1 Like

It works both ways
let sale = true;

//sale = false;

if (sale)

{

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

}

else

{

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

};

Both methods are valid or the exercise now, but which is best practice?

I use if (sale == true) {} because not every if statement is going to be comparing boolean values, and as far as I know, JScript is the only language that has this exception. Why?

That cleared a lot of my confusion thank you.

I think you mean to write if (sale === true) with 3 commas :))

To answer your question, I would like to know why as well, but as for which one is best practice, I recommend starting with if (sale === true) if that helps understanding it better! When you are more familiar with the topic, then it might be better to use the shorthand…

This was a fantastic explanation, thank you so much!

1 Like

That is a subtle but real difference - I’m struggling to think of a use case where you’d be looking for type equivalence as well.

Another thought - that also means that

if (sale)

is actually the same as

if (sale == true) //loose equality - really no reason to ever prefer this over the above

rather than

if (sale === true) // strict equality - check that sale is both truthy and of type bool

Actually I suppose the triple-equals form could be useful if you were pulling that sale value from a database or file and it could potentially be null or a junk value. Seems pretty contrived though :slight_smile:

Thank you so much mtf, you seem to talk an awful lot of sense. You certainly know the JavaScript terminology, which is rather impressive especially for those who are in our few first days of learning JS.

The if statement was driving me mad, now with your explanation and getting into if else statements things seem to be falling into place.

2 Likes

Ok, but how do you know it tests truthiness and not falsiness? Is it true by default or is it because there is a variable declared before the statement which declares it either true or false?

A test for truthiness is a test for falsiness. However we cannot write,

if (false) {

}

because the true branch will never be entered. We would need to NOT it, to get a true,

if (! true) {

}

Good explanation @mtf

1 Like