JS remove empty elements from an array | Example code

Use filter or Arrow function to Remove empty or whitespace or empty data from array in JavaScript.

JS remove empty elements from array example code

Let’s see HTML example code for it:-

Using Filter Function


JS remove empty elements from an array

Using Arrow function

You could use the filter one more way like:



Array(3) [ “One”, “Two”, “Four” ]

Because an empty string evaluates to boolean false. It works with all falsy values like 0, false, null, undefined, '', etc.

Note: If you want to keep some values like the number 0 (zero) you could use item !== undefined. This filters only undefined values. Keep in mind to trim your string or check with regex to ensure empty strings without whitespaces.

Source: Stackoverflow.com

Do comment if you have any doubts or questions on this topic.

Note: All JS Examples codes are tested on the Firefox browser and the Chrome browser.
OS: Windows 10
Code: HTML 5 Version

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.