How to remove null from the array element?

How to remove null from the array element?

function bouncer(arr) {
  // Don't show a false ID to this bouncer.
  arr = arr.filter(function (val){
      case false : 
      case null  : 
      case 0     :
      case ""    :
      case undefined : 
      case NaN  :
        return 0;
      default :
        return 1;
  return arr;

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


I’ve edited your post for readability. When you enter a code block into the forum, remember to precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard.


Remember this because it will probably come up as an interview question. NaN never equals itself.

NaN === NaN; false
NaN == NaN; false

There is a handy function that can tell you when something is NaN. It’s called… wait for it… isNaN.


This is a common misunderstanding with the Falsy Bouncer. You can make a solution for this challenge which tests against a hardcoded list of falsy values, but it misses the point of the exercise. The concept of values being “falsy” isn’t something made up by Free Code Camp for the purposes of this challenge, but rather a quality that values have in programming languages. A value is falsy if the language treats the value as false when it is evaluated as a boolean. This means that Boolean(someFalsyValue) is false but also that we can use it in a conditional like if (someFalsyValue). We can also talk about values being “truthy”. As I’m sure you’ve guessed, truthy values evaluate to true in a boolean of logical context.

if (someFalsyValue) {
    // this code block is not entered
else if (someTruthyValue) {
    // this code block is entered and executed