Can't Eliminate null Values - Falsy Bouncer

Can't Eliminate null Values - Falsy Bouncer
0.0 0

#1

My below code works at removing all the values but null. Does anyone know why?

var falsey = [false, null, 0,"",undefined,NaN];

function remove(val){
  return falsey.indexOf(val) == -1;
}

function bouncer(arr) {
  var filtered = arr.filter(remove);
  return filtered;
}

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

#2

Did you mean it doesn’t remove the NaN?
Check this out:

null == null
true
NaN == NaN
false

It does not remove NaN because when the indexOf function does not find it in the array.

console.log(falsey.indexOf(NaN))
//logs -1

There is a function called isNaN that would return true in this case, more details here

isNaN(NaN)
true

#3

You’re right! That was the issue. For some reason though, the console was returning the value to me as null not NaN. Any idea why?


#4

I’m going to be DRY and just link to yesterday’s discussion: