How to Remove Empty or Null Values From Array in JavaScript

In this topic, we are going to learn how to remove empty, null or undefined values from an array in JavaScript.

Consider an Array

Let’s consider an array:

let arrary  = ["data1", "data2", "", "10", "", undefined, 25, null, 40];

We are going to remove empty, undefined and null values from this array.

Remove Values

To remove these values we will use filter() method. Let’s use this:

let arrary  = ["data1", "data2", "", "10", "", undefined, 25, null, 40];

const filtered_array = arrary.filter(el => el);

console.log(filtered_array);

Now check the console log and the output looks like:

(5) ["data1", "data2", "10", 25, 40]