Removing Empty Strings from an array in JavaScript is not difficult, but it can not be easy with beginners if you don’t have the instructions. We will accompany you to solve this problem. There are some solutions can remove empty Strings from an array in JavaScript. Let’s learn about it with the explanation and examples below.
Remove Empty Strings From An Array In JavaScript
What is a String in JavaScript?
Like many programming languages, String is the most important type in JavaScript. A String is a sequence of characters. Strings in JavaScript are surrounded by either single quotation marks, double quotation marks, grave accents, or String objects in JavaScript.
Look at the example below to know more about it.
// create a string named 'str1' with single quotation marks.
let str1 = 'Hello everyone. This guide will help convert a String to the title case in JavaScript.'
// create a string named 'str1' with single quotation marks.
let str2 =" I am crvt4722."
// create a string named 'str3' with object String.
let str3 = new String(" Date: 11/09/2022.")
// create a string named ‘str4’ with grave accents.
let str4 = `Title: Convert A String To Title Case In JavaScript.`
console.log(str1)
console.log(str2)
console.log(str3)
console.log(str4)
Output
Hello everyone. This guide will help convert a String to the title case in JavaScript.
I am crvt4722.
String {' Date: 11/09/2022.'}
Title: Convert A String To Title Case In JavaScript.
What is an empty String in JavaScript?
An empty String is a String that length is 0. It doesn’t have any characters.
Look at the example below to know more about it.
// create an empty string named ‘str’
let str =''
Solution 1: Using filter() method in JavaScript
Syntax
array.filter(function(currentValue, index, arr), thisValue)
Parameters
- function(): A function runs for each element in the array.
- currentValue: The value of the current element.
- index: The index of the current element.
- arr: The array of the current element.
- thisValue: The value that passed the function.
Return Value
An array contains the elements which pass the function.
Look at the example below to know about this method.
// create an array named ‘arr’
const arr = ["Learn", "", "To", "", "Share", "", "IT"];
const res = arr.filter(e => e.length >0);
console.log(res);
Output
['Learn', 'To', 'Share', 'IT']
Solution 2: Using map() method to iterate the array
Syntax
array.map(function(currentValue, index, arr), thisValue)
Parameters
- function(): A function runs for each element in the array.
- currentValue: The value of the current element.
- index: The index of the current element.
- arr: The array of the current element.
- thisValue: The value that passed the function.
Return Value
An array contains the elements which pass the function.
Look at the example below to know about this method.
//create an array named ‘arr’.
const arr = ["Learn", "", "To", "", "Share", "", "IT"];
const res = [];
arr.map(value => {
if (value !== '') {
res.push(value);
}
});
console.log(res);
Output
['Learn', 'To', 'Share', 'IT']
Summary
These are some solutions that can help you remove empty Strings from an array in Javascript. To solve this problem, you can use JavaScript Array Iteration, such as the filter() method and the map() method like above. Choose the solution that is the most suitable for you. We hope this tutorial is helpful to you. Have an exciting learning experience. Thanks!
Maybe you are interested:
- Find the First Array Element matching a Condition in JS
- How to Empty an Array in JavaScript
- Create an array containing 1 to n numbers in javascript

My name is Thomas Valen. As a software developer, I am well-versed in programming languages. Don’t worry if you’re having trouble with the C, C++, Java, Python, JavaScript, or R programming languages. I’m here to assist you!
Name of the university: PTIT
Major: IT
Programming Languages: C, C++, Java, Python, JavaScript, R