How to Remove Null Values from Array in JavaScript

When working with real-time data, you might get an array that includes null values inside. You surely want to remove those null values before going further for calculations.

Removing Null Values From the Array with filter method

The simplest solution to this problem is to use the filter method on array that contains null values. You can check for the null values with if conditions inside the callback. Here is a simple example to remove null values from array.

Removing Null values From Array with Short Code

You can make the above code simpler by using Boolean as an argument to filter function. The Boolean argument will filter all the falsy values like null, undefined, false from the array. The following is the shortest code to remove false values from array.

You can read more about filter function in JavaScript.

Conclusion

You can remove undefined or falsy values from an array with filter function in JavaScript. The filter function can take a callback function or just Boolean to filter all the null or falsy values.

Latest articles

Related articles