Languages
[Edit]
EN

JavaScript - remove empty strings from array

3 points
Created by:
Saim-Mccullough
688

In this article, we would like to show you how to remove empty strings from an array in JavaScript.

Quick solution:

const outputArray = inputArray.filter(x => typeof x !== 'string' || x.length > 0);

 

Practical example

In this example, we use filter() method to create a copy of the letters array without empty strings.

// ONLINE-RUNNER:browser;

const letters = ['A','', "",,,, 'B', 'C', 0, 1, 2];
const result = letters.filter(x => typeof x !== 'string' || x.length > 0);

console.log(result);  // ['A', 'B', 'C', 0, 1, 2]

See also

  1. JavaScript - remove empty elements from array

References

  1. Array.prototype.filter() - JavaScript | MDN
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join