[Solved] Why does this not work? (AKA Why does THIS work?) Re: Boo Who

Okay, so after looking at the answer I know I could have easily solved this with typeof, but because my memory is horrible, my original answer looked like this:

function booWho(bool) {
   return bool === (true || false) ? true : false;
 }

However booWho(false) would always return false.

What I ended up doing to get it to work is this:

function booWho(bool) {
  return bool === true ? true : bool === false ? true : false;
}

So why does my second solution work while my first doesn’t? Both should be making the comparison false === false and returning true, but only one works and the other doesn’t.

Thanks in advance for any insight!

Well (true || false) is always true.

1 Like

it is because you can’t compare one thing with two different ones, so when you have bool === (true || false) first the thing between parenthesis is evaluated, and it becomses true, so you have bool === true - if you want to do it with the OR operator you need to do two different comparisons: bool === true || bool === false

1 Like

Ah! I feel like a dummy! Thanks for your quick answer!

You’re right! I wasn’t making the comparison properly. Duh! Thanks for your quick answer!

Also, you don’t need the whole ternary operator as you are just returning true or false, just a comparison that evaluates to a boolean value is enough

you are saying “if this is true return true”, you can just return “this” (as it will be or true or false) and it works the same

1 Like