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.

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