In JavaScript, strings are immutable, which means the best you can do is to create a new string with the changed content and assign the variable to point to it.

You'll need to define the replaceAt() function yourself:

String.prototype.replaceAt = function(index, replacement) {
    return this.substring(0, index) + replacement + this.substring(index + replacement.length);
}

And use it like this:

var hello = "Hello World";
alert(hello.replaceAt(2, "!!")); // He!!o World
Answer from Cem Kalyoncu on Stack Overflow
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ how-to-replace-a-character-at-a-particular-index-in-javascript
How to replace a character at a particular index in JavaScript ? | GeeksforGeeks
April 28, 2023 - The second part of the string can be extracted by using the starting index parameter as โ€˜index + 1โ€™, which denotes the part of the string after the index of the character. The second parameter is omitted to get the whole string after it. The new string is created and concatenates the two parts of the string with the character to be replaced and added in between. This will create a new string with the character replaced at the index.
Discussions

javascript - Using .replace() at a specific index - Stack Overflow
From the "related questions" bar, this old answer seems to be applicable to your case. Replacing a single character (in my referenced question) is not very different from replacing a string. How do I replace a character at a particular index in JavaScript? More on stackoverflow.com
๐ŸŒ stackoverflow.com
August 9, 2016
javascript - How to find index on a string and replace? - Stack Overflow
792 How do I replace a character at a specific index in JavaScript? ... 1 trying to replace the index of a string with a character of another string in the corresponding index More on stackoverflow.com
๐ŸŒ stackoverflow.com
Javascript using .replace() at a specific index of the search - Stack Overflow
Converted string to array by split and using map returned each element as it is and just replaced word when reached to specific index (by logic of index-- == 0). More on stackoverflow.com
๐ŸŒ stackoverflow.com
javascript - Replace character at string index - Stack Overflow
Why won't this code work for me below? Can't i reassign a value at a specific index in the string? function replaceChar (str) { let i for (i = 0; i More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
Bobby Hadz
bobbyhadz.com โ€บ blog โ€บ javascript-string-replace-character-at-index
Replace a Character at a specific Index in JavaScript | bobbyhadz
The String.split() method takes the following 2 parameters: The next step is to use bracket notation to replace the character at the specified index.
๐ŸŒ
W3docs
w3docs.com โ€บ home โ€บ code snippets โ€บ javascript โ€บ how to replace a character at a particular index in javascript
How to Replace a Character at a Particular Index in JavaScript | W3docs
The first method is split and join which converts the string into an array and replaces the character at the specified index. The string is converted into an array by using <kbd class="highlighted">split()</kbd> with the separator as a blank ...
๐ŸŒ
thisPointer
thispointer.com โ€บ home โ€บ javascript โ€บ javascript: replace a character in string at given index
Javascript: Replace a character in string at given index - thisPointer
July 1, 2021 - ... function replaceAtIndex(_string,_index,_newValue) { split_string = _string.substring(0, _index) + ' ' + _string.substring(_index + 1); return_string = split_string.split(''); return_string[_index] = '*'; return_string = return_string.join(''); ...
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ article โ€บ How-do-I-replace-a-character-at-a-particular-index-in-JavaScript
How do I replace a character at a particular index in JavaScript?
July 20, 2022 - This method converts the string to an array, modifies the character at the target index, and then joins the array back into a string. let charArray = [...string]; // convert string to array charArray[index] = newCharacter; // replace character ...
๐ŸŒ
Code Highlights
code-hl.com โ€บ home โ€บ javascript โ€บ tutorials
Master Javascript Replace at Index in 3 Easy Steps | Code Highlights
February 11, 2024 - For instance, if you wish to change a value at an index in a string in JavaScript, you'll need to slice and concatenate parts of your original string around the index where the replacement is needed.
๐ŸŒ
Dirask
dirask.com โ€บ posts โ€บ JavaScript-replace-part-of-string-from-given-index-jPvAmD
JavaScript - replace part of string from given index
// ONLINE-RUNNER:browser; function repeatText(count, text) { var result = ''; for (var i = 0; i < count; ++i) { result += text; } return result; } function replaceText(text, insert, index, spacer) { if (spacer == null) { spacer = ' '; } var prefix = text.substring(0, index); var postfix = text.substring(index + insert.length); var space = repeatText(index - text.length, spacer); return prefix + space + insert + postfix; } // Usage example: console.log( replaceText('This is text.', '__', 0) ); // __is is text.
Find elsewhere
๐ŸŒ
SitePoint
sitepoint.com โ€บ blog โ€บ javascript โ€บ replace string characters in javascript
Replace String Characters in JavaScript โ€” SitePoint
February 13, 2024 - However, you can achieve this by splitting the string into an array of characters, replacing the character at the desired index, and then joining the array back into a string: let str = "Hello, World!"; str = str.split(''); str[7] = 'E'; str = str.join(''); console.log(str); // "Hello, Eorld!"
๐ŸŒ
GitHub
gist.github.com โ€บ efenacigiray โ€บ 9367920
Javascript replace char at index ยท GitHub
April 6, 2021 - return string.substring(0, index) + replace + string.substring(index + 1);
๐ŸŒ
GitHub
github.com โ€บ bobbyhadz โ€บ javascript-string-replace-character-at-index
GitHub - bobbyhadz/javascript-string-replace-character-at-index: A repository for an article at https://bobbyhadz.com/blog/javascript-string-replace-character-at-index
The JS code is in the index.js file. To be able to run the code, follow these instructions: Clone the GitHub repository with the git clone command. Open your terminal in the project's root directory (right next to package.json). Install the node modules. ... To run the code, issue the npm start command. ... Alternatively, you can run the project in watch mode, so every time you save, the JavaScript server is restarted.
Author: bobbyhadz
๐ŸŒ
xjavascript
xjavascript.com โ€บ blog โ€บ how-do-i-replace-a-character-at-a-specific-index-in-javascript
How to Replace a Character at a Specific Index in JavaScript: Step-by-Step Guide โ€” xjavascript.com
The slice() method extracts a portion ... then concatenate them with the new character in between. Extract the substring before the index using slice(0, index)....
๐ŸŒ
JavaScript in Plain English
javascript.plainenglish.io โ€บ how-to-replace-a-character-at-a-particular-index-in-javascript-3375246c0ff8
How to Replace a Character at a Particular Index in JavaScript? | by John Au-Yeung | JavaScript in Plain English
December 26, 2021 - And we can extract the string from the index to the end of the string. Then we can add the replacement between the 2 substrings. ... const replaceAt = (str, index, replacement) => { return str.substr(0, index) + replacement + str.substr(index + 1); }const str = 'a b c' const newStr = replaceAt(str, 2, 'foo') console.log(newStr)