How to Remove Empty or Null Values From Array in JavaScript

Published on January 5, 2020 19 sec read

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]

Monthly Newsletter

One email a month, packed with the latest tutorials, delivered straight to your inbox.
We'll never send any spam or promotional emails.
Author

Hey, I'm Md Obydullah. I build open-source projects and write article on Laravel, Linux server, modern JavaScript and more on web development.

Follow