How to remove a character from a position of a string in javascript?
In JavaScript, we can easily remove a character from a particular position from a string. Let us consider the string JavaScript is the best
. Now we want to remove a character from the 5th position.
We can do it multiple ways:
Approach #1
const position = 5; let str = "JavaScript is the best"; str = str.slice(0, position) + str.slice(position + 1); console.log(str); //
Approach #2
const position = 5; let str = "JavaScript is the best".split(''); str.splice(position, 1); str = str.join('');
Approach #3
const position = 5; let str = "JavaScript is the best"; const newStr = str.substring(0, position - 1) + str.substring(position, str.length); console.log(newStr);