Falsies not Falsey

Tell us what’s happening:
Why Are Falsey not false?

Your code so far


function bouncer(arr) {
  for(let each in arr){
    console.log(arr[each], arr[each] == false)
  }
}

bouncer([false, null, 0, NaN, undefined, ""]);


false,true
,false
0,true
NaN,false
,false
,true

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36.

Link to the challenge:

some falsy values have unusual behaviours, so you can’t just compare them like that. But if left on their own they evaluate to false when evaluated to a boolean. Try with the NOT operator !, you will see!

!null     // true
!!null    // false
!""       // true
!!""      // false

The value they evalute to with the double NOT !! is the value they evaluate to if evalaute as a boolean, for example in a if statement condition (if (value) {} - here falsy values will make the statement not execute)

Thanks, that is strange. Is it best practice to use the double not for true/false boolean comparisons?

I don’t know how I’ve never run into this before.

You can just use the value as it is (this is what the challenge wanted to show) - or you can use the double not, which I was using to show how the values evaluate

Example with truthy values:

!1    // false
!!1    // true
!'String'   // false
!!'String'   // true
1 Like

ooooh “use the value as it is”. Duh, I knew that. Sometimes I get tunnel vision.

Here is a bit more info


1 Like