• Skip to primary navigation
  • Skip to main content

The Programming Expert

Solving All of Your Programming Headaches

  • Home
  • Learn to Code
    • Python
    • JavaScript
  • Code Snippets
    • HTML
    • JavaScript
    • jQuery
    • PHP
    • Python
    • SAS
    • Ruby
  • About
  • Write for Us
You are here: Home / JavaScript / Remove String From Array in JavaScript

Remove String From Array in JavaScript

September 12, 2022 Leave a Comment

To remove a string from an array in JavaScript, the easiest way is to use the JavaScript Array filter() method.

Below shows an example of how you could remove the string “hello” from an array of strings with filter() method.

var array_of_strings = ["hello","goodbye","Hello","hey","hi"];

var filtered_array = array_of_strings.filter(x => x != "hello");

console.log(filtered_array);

// Output:
['goodbye', 'Hello', 'hey', 'hi']

If you want to be more explicit with the callback function to filter(), you can do the following.

var array_of_strings = ["hello","goodbye","Hello","hey","hi"];

var filtered_array = array_of_strings.filter(function(x) {
  return x != "hello";
});

console.log(filtered_array);

// Output:
['goodbye', 'Hello', 'hey', 'hi']

When working with arrays in JavaScript, it can be valuable to be able to easily filter and remove unwanted values from your array.

One such situation is where you may want to remove all instances of a string from an array.

We can easily remove a string from an array in JavaScript using the JavaScript array filter() method.

To use filter(), you need to pass a callback function which will provide logic to identify the value or values you want to remove.

Below are a few more examples of how you can use the filter() method to filter out a string from arrays in JavaScript.

If you have an array of strings, you can easily filter out any strings with filter().

var array_of_strings = ["This","toRemove","is","an","array","toRemove","with","strings","toRemove","we","want","to","remove","."];

var filtered_array = array_of_strings.filter(x => x != "toRemove");

console.log(filtered_array);

// Output:
['This', 'is', 'an', 'array', 'with', 'strings', 'we', 'want', 'to', 'remove', '.']

Using a For Loop to Remove a String From Array in JavaScript

We can also remove a string from an array using a for loop with an if conditional statement. Here is a simple function we will create to remove a string from an array.

function removeString(str,arr){
  var new_array = [];
  for (var i=0; i<arr.length; i++){
    if( arr[i] != "str" ){
      new_array.push(arr[i]);
    }
  }
  return new_array;
};

Let’s see this code in action with our simple example from above.

function removeString(str,arr){
  var new_array = [];
  for (var i=0; i<arr.length; i++){
    if( arr[i] != str ){
      new_array.push(arr[i]);
    }
  }
  return new_array;
};

var array_of_strings = ["This","toRemove","is","an","array","toRemove","with","strings","toRemove","we","want","to","remove","."];

array_of_strings = removeString("toRemove",array_of_strings);

console.log(array_of_strings);

// Output:
['This', 'is', 'an', 'array', 'with', 'strings', 'we', 'want', 'to', 'remove', '.']

Hopefully this article has been useful for you to learn how to remove a string from array in JavaScript.

Other Articles You'll Also Like:

  • 1.  Convert String to Float in JavaScript
  • 2.  Set Text with the textContent Property in JavaScript
  • 3.  JavaScript Capitalize First Letter of Every Word
  • 4.  Using JavaScript to Check if String Contains Letters
  • 5.  Using JavaScript to Reverse a Number
  • 6.  Using JavaScript to Add Trailing Zeros
  • 7.  JavaScript Opacity – How to Change the Opacity of an Element Using JavaScript
  • 8.  JavaScript onfocusout – How to Use the onfocusout Event on an HTML Form
  • 9.  Changing the Background Image of a div in JavaScript
  • 10.  How Long Does It Take to Learn JavaScript?

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 *

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