To remove empty strings from an array using JavaScript, one of the easiest ways to do this is to use a for loop with an if conditional statement. Here is a simple function we will create to remove empty strings from an array.
function removeEmptyStrings(arr){
var new_array = [];
for (var i=0; i
Notice above we also make use of the push() method. This simply adds the element to the end of the array.
And here is our function in use with an example array.
var array_of_strings = ["This","","is","an","array","","with","empty","","strings","."]
function removeEmptyStrings(arr){
var new_array = [];
for (var i=0; i
When working with arrays of strings, it can be valuable to be able to easily filter and remove unwanted values from your array.
One such situation where you may want to remove values from an array is if you have a lot of empty strings in your array.
We can easily remove all empty strings from an array using JavaScript by simply iterating over the array and removing the empty strings. This is done simply with the use of a for loop and an if conditional statement.
Below again is the code for our function which will remove all instances of empty strings from an array in JavaScript.
function removeEmptyStrings(arr){
var new_array = [];
for (var i=0; i
Removing Any Value from an Array Using JavaScript
In a very similar way, we can remove any value from an array using the same technique we used in our function above.
For example, if we instead wanted to remove all zeros from a array, we could do that easily in JavaScript by adjusting the code above.
function removeZeros(arr){
var new_array = [];
for (var i=0; i
And here it is with some example code:
var array_of_numbers = [1,0,4,2,-4,0,0,3,0,-1,0];
function removeZeros(arr){
var new_array = [];
for (var i=0; i
Here is another quick example. We have an array of strings and we want to remove the word "whoa".
function removeString(arr){
var new_array = [];
for (var i=0; i
var array_of_strings = ["whoa","there","hey","there","whoa"]
function removeString(arr){
var new_array = [];
for (var i=0; i
Hopefully this article has been useful for you to learn how to remove empty strings from an array in JavaScript.