javascript replace at index code example

Example 1: js replace item in array at index

array.splice(indexWhereToReplace, 1, replacement)

// Ex
const arr = [1, 2, 3]
arr.splice(1, 1, 'replacement')
console.log(arr) // [1, 'replacement', 3]

Example 2: javascript string change character at index

String.prototype.replaceAt=function(index, char) {
    var a = this.split("");
    a[index] = char;
    return a.join("");
}

Example 3: string one char change in typescript

let hello:string = "Hello World";
console.log(hello.replaceAt(2, "!!")); // Should display He!!o World