How to use splice() method In Javascript
The JavaScript Array splice() Method is an inbuilt method in JavaScript that is used to modify the contents of an array by removing the existing elements and/or by adding new elements.
Syntax:
Array.splice( index, remove_count, item_list )
Example:
// Input string
let str = 'GeeksGeeks'
// Input Substring
let subStr = 'for'
// Index to add substring
let pos = 5
// Convert to array of string
let arr = str.split('')
// Add substring at given position
arr.splice(pos, 0, ...subStr)
// Display result
console.log(arr.join(''))
Output
w3wiki
JavaScript Insert a string at position X of another string
Given two strings, the task is to insert one string in another at a specified position using JavaScript. We’re going to discuss a few methods, these are: