Open In App

JavaScript - Insert Character in JS String

Last Updated : 22 Nov, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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 Beginning

To insert a character at the beginning of a string, you can use string concatenation or template literals

JavaScript
let str = "GFG";
let ch = "H";
let res = ch + str;
console.log(res);

Output
HGFG

You can insert the character at the beginning of the given string using many other methods. Refer to this article for more methods.

At the End

To add a character to the end of a string, concatenation or template literals are often used.

JavaScript
let str = "GFG";
let ch = "H";
let res = str + ch;
console.log(res);

Output
GFGH

You can insert the character at the end of the given string using many other methods. Refer to this article for more methods.

At a Given Position

To insert a character at a specific position, split the string into two parts and concatenate the character between them.

JavaScript
let str = "GFG";
let ch = "H";
let res = str.slice(0, 1) + ch + str.slice(1);
console.log(res);

Output
GHFG

You can insert the character at a specific position in the given string using many other methods. Refer to this article for more methods.


Next Article

Similar Reads