To replace a character at a specific index in a string in JavaScript, you can convert the string to an array, modify the character at the desired index, and then join the array back to a string.
In this example,
str
.split('')
method.index
with the character newChar
.join('')
method.str
.let str = 'Hello World';
let index = 6;
let newChar = 'x';
let strArray = str.split('');
strArray[index] = newChar;
str = strArray.join('');
console.log(str);
Hello xorld
In this tutorial, we learned How to Replace Character at Specific Index in String in JavaScript language with well detailed examples.