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]

Author

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

Follow

Leave a Reply

Your email address will not be published. Required fields are marked *