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

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]

You're welcome to suggest any article to write!

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

Leave a Reply

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