Anjan Dutta

Remove a character from string in javascript

Remove a character from string in javascript

Remove character from a specific position

To remove a character from a specific position of a string in javascript, we can use the substring() method like below.

function remove_character_from_position(str, pos) {
return str.substring(0, pos) + str.substring(pos + 1, str.length);
}
console.log(remove_character_from_position("Hello World",5));
// > HelloWorld

Remove all occurrences of a character

To remove all occurrences of a character from a string in javascript, we can use the replace() method.

Using replace()

The replace() method replaces a character with another character.

The below example code shows how we can use the replace() function to remove a character.

let str = "Hello World";
str.replace("o", "");
console.log(str);
// > Hell Wrld