How to Remove a Character from a String JavaScript

Remove a Character from a string JavaScript

In this article, we have learned how to remove a character from a string javascript. There are many ways to remove characters from strings, we can see the simple methods of it.

We can use replace() function to remove the character string. It is one of the best and easiest methods. The replace() function is used to replace the selected character with a given character in the string. We can pass two parameters of replace() function, the search string, and the new string.

syntax:

stringvariable.replace(search value, new value);

Ex:

var name = "Wonder Develop";
document.write("Before Removed Character : "+name+"<br/>");
var name = name.replace("W"," ");

document.write("After Removed Character : "+name);

Output:

remove character from string in JS

  1. See the above example code, we have created a variable “name” and assigned a string value.
  2. I want to remove the “W” letter in the “name” string. So, we use replace() function.
  3. To use replace() function, we must put a string variable called “name” then put the (.) dot operator and then use replace() function. Ex: name.replace().
  4. To remove characters, we must pass two parameters. First, pass what character you want to remove and then pass an empty string.
  5. See the above output, we remove the character “W” successfully.

Learn also: How to remove the first character from string >> click here

Leave a Reply