<
h1
style
=
"color: green"
>
GeeksforGeeks
</
h1
>
<
b
>
How to replace a character at a
particular index in JavaScript?
</
b
>
<
p
>
The character at the 8th index
would be replaced by "M".
</
p
>
<
p
>
Original string is: GeeksforGeeks
</
p
>
<
p
>
New String is:
<
span
class
=
"output"
></
span
>
</
p
>
<
button
onclick
=
"changeText()"
>
Replace Character
</
button
>
<
script
>
function replaceChar(origString, replaceChar, index) {
let firstPart = origString.substr(0, index);
let lastPart = origString.substr(index + 1);
let newString =
firstPart + replaceChar + lastPart;
return newString;
}
function changeText() {
originalText = "GeeksforGeeks";
charReplaced =
replaceChar(originalText, "M", 8);
document.querySelector('.output').textContent
= charReplaced;
}
</
script
>