How to Remove Characters from Strings in JavaScript

JavaScript provides users with various methods and properties for string manipulation, to transform those strings or to search useful information from those strings. Sometimes we have various lines of code in which we need to make changes, search for a character or replace a character or remove a character from a string.

All these tasks become difficult to do and hence methods are provided by JavaScript that makes the job easier. Users can easily use these methods to manipulate a string and transform it. In this article we’ll discuss how to remove characters from strings in JavaScript, various ways and methods provided by JavaScript along with examples for your better understanding.

Remove Characters from Strings

JavaScript provides various in-built methods to remove characters from a string, some of which are listed below:

Using substring() method

The method substring() in JavaScript takes in two parameters, the starting and the ending indexes and returns a substring as an output. You can also pass only one parameter, the starting index and it’ll split the string from that index mentioned till the end of the string, here below is the example:

const example = "Welcome To the website!"





Using substr() method

Another method similar to substring() method is substr() that takes in two parameter starting and ending indexes and retrieves the characters between these specified indexes. Below is the example:

const example = "  Welcome To the website!"






Using replace() method

Another method in JavaScript is replace() method that takes in two parameters, the first one is the character to replace and the second one is the character to replace the character with. The output is a new string with the replaced values, below is an example:

const example = "Welcome To the website!"

console.log(example.replace("the", "our"));


console.log(example.replace("e"," "));


Using replace() method with Regular Expression

As we saw in the above replace() method example, when we wanted to remove the “e” character with whitespace, it only replaced the first occurring character. But what if we want to replace all the “e” characters or any character in the whole string with our desired character?

Well, for that we use Regular expression which provide us with various modifiers such a s global modifies “/g” that searches the whole string and replace all the particular mentioned character in the entire string, below is an example:

const example = "Welcome To the website!"

console.log(example.replace("e"," "));

console.log(example.replace(/e/g," "));


Using slice() method

The slice() method works in a similar way as substring() and the substr() method, it takes in two parameters where we define the starting index and ending index of the part which we want to slice from the string. The output is the sliced string, below is an example:

const example = "  Welcome To the website!"

console.log(example.slice(5, 9));

console.log(example.slice(1, 2));

console.log(example.slice(1, example.length-1));

console.log(example.slice(2, example.length-1));


Using split() method

Another method JavaScript provides for removing characters is split() method which is used along with join() method. Firstly we use split() method to remove our desired character and it returns an array of strings. After that join() method is used to join the string, demonstrated below by an example:

const example = "  Welcome To the website!"

console.log(example.split(" ").join(""));




Removing a specific character from a string can be difficult on your own sometimes, and hence methods are provided by JavaScript to manipulate string and remove characters from it. In this article we discussed various built-in methods in JavaScript for removing chcahters or part of a string from the whole string, along with examples. These methods are useful, and make the task of removing characters easy while dealing with lengthy codes.

About the author

Shehroz Azam

A Javascript Developer & Linux enthusiast with 4 years of industrial experience and proven know-how to combine creative and usability viewpoints resulting in world-class web applications. I have experience working with Vue, React & Node.js & currently working on article writing and video creation.