remove null from array javascript
- The
filter()
method creates a new array with all elements that pass the test implemented by the provided function.- The filter() method creates a new array filled with elements that pass a test provided by a function.
javascript remove null from array
- To remove empty, null, or undefined elements from an array, we can use the filter() array method.
- Then, pass a function to the method which returns the element currently getting looped.
Return value
- A new array with the elements that pass the test.
- If no elements pass the test, an empty array will be returned.
remove null from array
Example 1
arr.filter(Boolean)
const array = [23, "😂", null, undefined, 0, false, NaN, '', [], null, {}]; console.log(array.filter(Boolean))
Output
[ 23, "😂", [], {} ]