JavaScript - Remove empty values from array
A JavaScript code example for removing empty values from an array.
The HTML has an output element with id "output" to display the result.
<p id="output"></p>
The filter()
method can be used to remove empty values from an array.
By specifying the "Boolean" to filter out as the filter()
argument, it returns the array with empty values removed.
let array = ['Apple', '', 'Banana', 'Grape', ''];
// Remove empty values from array
array = array.filter(Boolean);
console.log(array);
document.querySelector('#output').innerHTML = array;