JavaScript - Insert Character at a Given Position in a String Last Updated : 21 Nov, 2024 Comments Improve Suggest changes Like Article Like Report These are the following approaches to insert a character at a given position: 1. Using String Concatenation with SlicingSplit the string into two parts—before and after the position—and concatenate the character in between. JavaScript let str = "Hello GFG"; let ch = "!"; let idx = 5; let res = str.slice(0, idx) + ch + str.slice(idx); console.log(res); OutputHello! GFG 2. Using Template LiteralsUsing template literals, we can easily insert a character at a specified position in a string by breaking the string into two parts and injecting the new character between them. JavaScript let str = "Hello GFG"; let ch = "!"; let idx = 5; let res = `${str.slice(0, idx)}${ch}${str.slice(idx)}`; console.log(res); OutputHello! GFG 3. Using Arrays for InsertionConvert the string into an array, insert the character at the desired position using array manipulation, and then join the array back into a string. JavaScript let str = "Hello GFG"; let ch = "!"; let idx = 5; let arr = str.split(""); arr.splice(idx, 0, ch); let res = arr.join(""); console.log(res); OutputHello! GFG Comment More infoAdvertise with us Next Article JavaScript - Insert Character at a Given Position in a String M meetahaloyx4 Follow Improve Article Tags : JavaScript Web Technologies javascript-string Similar Reads JavaScript - Insert Character in JS String In JavaScript, characters can be inserted at the beginning, end, or any specific position in a string. JavaScript provides several methods to perform these operations efficiently.At the BeginningTo insert a character at the beginning of a string, you can use string concatenation or template literals 1 min read JavaScript - Insert a String at a Specific Index These are the following methods to insert a string at a specific index in JavaScript:1. Using the slice() MethodInserting a string using the slice() method involves splitting the original string into two parts: one before the insertion point and one after. The new string is then placed between these 3 min read 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: Methods to Insert String at a Certain IndexTable of Content Method 1: Using JavaScript String slice() and Array join() MethodsMethod 2: JavaScript 4 min read JavaScript - Add a Character to the End of a String These are the following ways to insert a character at the end of the given string:1. Using ConcatenationWe can use the + operator or template literals to append the character.JavaScriptlet str = "Hello GFG"; let ch = "!"; let res = str + ch; console.log(res); OutputHello GFG! 2. Using Template Liter 2 min read How to replace a character at a particular index in JavaScript ? To replace a character from a string there are popular methods available, the two most popular methods we are going to describe in this article. The first method is by using the substr() method. And in the second method, we will convert the string to an array and replace the character at the index. 4 min read Like