• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / JavaScript / Remove Empty Strings from an Array in JavaScript

Remove Empty Strings from an Array in JavaScript

May 23, 2022 Leave a Comment

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<arr.length; i++){
    if( arr[i] != "" ){
      new_array.push(arr[i]);
    }
  }
  return new_array;
};

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<arr.length; i++){
    if( arr[i] != "" ){
      new_array.push(arr[i]);
    }
  }
  return new_array;
};

console.log(removeEmptyStrings(array_of_strings));

#Output:
['This', 'is', 'an', 'array', 'with', 'empty', 'strings', '.']

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<arr.length; i++){
    if( arr[i] != "" ){
      new_array.push(arr[i]);
    }
  }
  return new_array;
};

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<arr.length; i++){
    if( arr[i] != 0 ){
      new_array.push(arr[i]);
    }
  }
  return new_array;
};

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<arr.length; i++){
    if( arr[i] != 0 ){
      new_array.push(arr[i]);
    }
  }
  return new_array;
};

console.log(removeZeros(array_of_numbers));

#Output:
[1, 4, 2, -4, 3, -1]

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<arr.length; i++){
    if( arr[i] != "whoa" ){
      new_array.push(arr[i]);
    }
  }
  return new_array;
};
var array_of_strings = ["whoa","there","hey","there","whoa"]
function removeString(arr){
  var new_array = [];
  for (var i=0; i<arr.length; i++){
    if( arr[i] != "whoa" ){
      new_array.push(arr[i]);
    }
  }
  return new_array;
};

console.log(removeString(array_of_strings));

#Output:
['there', 'hey', 'there']

Hopefully this article has been useful for you to learn how to remove empty strings from an array in JavaScript.

Other Articles You'll Also Like:

  • 1.  parseFloat JavaScript – How to use the JavaScript parseFloat() method
  • 2.  Using JavaScript to Remove Substring From String
  • 3.  Using JavaScript to Remove Null Values From an Array
  • 4.  Using JavaScript to Remove All Pipe Characters From String
  • 5.  Using Javascript to Check if Value is Not Undefined
  • 6.  Using JavaScript to Convert Float to Integer
  • 7.  JavaScript substring – How to Get a Substring From a String
  • 8.  Convert an Array to Set in JavaScript
  • 9.  Using JavaScript to get Textarea Value
  • 10.  Using JavaScript to Return Two Values

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy