• 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 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.  Using JavaScript to Get the Page Title
  • 2.  Insert Character Into String In JavaScript
  • 3.  Using JavaScript to Count Even Numbers in an Array
  • 4.  Using JavaScript to Get the Max Value in an Array
  • 5.  Using JavaScript to Get Date Format in dd mm yyyy
  • 6.  Using JavaScript to Subtract Days From a Date
  • 7.  How to Repeat Character N Times in JavaScript
  • 8.  How to Count Vowels in a String Using JavaScript
  • 9.  Using JavaScript to Check if Variable is a String
  • 10.  JavaScript isInteger – How to Check if a Number is an Integer

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