Remove empty elements from an array in Javascript
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
JavaScript arrays can contain "empty" elements — null, undefined, empty strings "", 0, false, NaN, or sparse array holes. The method you choose depends on which values you consider "empty." filter(Boolean) removes all falsy values, filter(x => x !== undefined) targets specific types, and flat() removes sparse holes. Understanding falsy values in JavaScript is key to choosing the right approach.
Remove All Falsy Values with filter(Boolean)
Boolean is a constructor function that returns false for falsy values and true for truthy values. Passing it to filter removes all falsy elements:
| Value | Boolean(value) | Removed? |
null | false | Yes |
undefined | false | Yes |
"" | false | Yes |
0 | false | Yes |
false | false | Yes |
NaN | false | Yes |
"hello" | true | No |
42 | true | No |
[] | true | No |
{} | true | No |
Remove Only null and undefined
The loose equality x != null checks for both null and undefined (they are loosely equal to each other) while keeping other falsy values like 0, "", and false.
Remove Only Empty Strings
Remove Sparse Array Holes
JavaScript arrays can have "holes" — indices with no value at all (different from undefined):
Using reduce for Custom Filtering
Removing Empty Values from Arrays of Objects
In-Place Modification
All previous examples create new arrays. To modify in place, iterate backwards:
Iterating backwards prevents index shifting from affecting unprocessed elements.
Chaining with map
A common pattern — map values and then filter out the empty results:
Common Pitfalls
filter(Boolean)removes0andfalse: If your array legitimately contains0orfalse,filter(Boolean)strips them out. Usefilter(x => x != null)to keep falsy values that are not null/undefined.- Confusing sparse holes with
undefined: A sparse array hole ([1, , 3]) is not the same asundefined.filter,map, andforEachskip holes entirely, butArray.fromconverts them toundefined. The behavior differs by method. - Forgetting that
filterreturns a new array:arr.filter(Boolean)does not modifyarr. If you need to update the original, assign the result back:arr = arr.filter(Boolean)or usesplicefor in-place modification. - Using
==instead of===for empty string checks:0 == ""istruein JavaScript due to type coercion. Use strict equality (===) when checking for specific empty values to avoid accidentally removing numbers. - Not handling whitespace strings:
" "(spaces, tabs, newlines) is truthy and passesfilter(Boolean). If whitespace-only strings should be treated as empty, usex.trim() !== ""orx.trim().length > 0.
Summary
filter(Boolean)removes all falsy values (null, undefined, 0, "", false, NaN)filter(x => x != null)removes only null and undefined, keeping 0, "", and falsefilter(x => x !== "")targets empty strings specifically- Sparse array holes are skipped by
filter,map, andforEachautomatically - Use strict equality (
===) to avoid type coercion surprises filtercreates a new array — usesplicewith backward iteration for in-place modification
Related reading
- Remove empty strings from a list of strings
- Remove item from list based on condition
- Remove Item in Dictionary based on Value
- Remove items from one list in another
- remove grey background on link clicked in ios safari / chrome / firefox
- Remove HTML tags from a String
- Remove last item from array
- Remove multiple keys from Map in efficient way?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.