js string insert at position code example

Example 1: js insert string at position

var a = "I want apple";
var b = " an";
var position = 6;
var output = [a.slice(0, position), b, a.slice(position)].join('');
console.log(output);

Example 2: javascript add string to middle of string

function addStr(str, index, stringToAdd){
  return str.substring(0, index) + stringToAdd + str.substring(index, str.length);
}

let str = "This is a string";
let stringToAdd = "modyfied ";

console.log(addStr(str, 10, stringToAdd));  //outPut : "This is a modified string"