Open In App

Add Characters to a String in JavaScript

Last Updated : 01 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In JavaScript, A string is a combination of multiple characters joined together to form a meaningful word. It can be a name or anything else. You can add an extra character to a string after it is defined to complete it or to practice adding characters.

There are several ways available in JavaScript to achieve this task as listed below:

Using ‘+’ operator

The + operator can be used to concat or add an extra character to the string by using it in between the string and the character to be added.

NOTE: It allows you to add characters either at the start or at the end of a string.

Syntax:

"initialString" + "extraCharacter";

Example: The below code implements the + operator to add a character to a string in JavaScript.

Javascript




const str1 = "GeeksforGeek" + "s";
const str2 = "J" + "avaScript";
console.log(str1, str2);


Output

GeeksforGeeks JavaScript

Using the concat() method

The string concat() method in JavaScript is used to merge two or more strings and returns a new string that is the result of a combination of the specified string. We can also use it to add a character to the string.

NOTE: It allows you to add characters only at the end of the string.

Syntax:

initialString.concat('extraCharacter');

Example: The below code uses the concat() method to add character to a string in JavaScript.

Javascript




const str1 = "GeeksforGeek";
const combStr1 = str1.concat('s');
const str2 = "JavaScrip";
const combStr2 = str2.concat('t');
console.log(combStr1, combStr2);


Output

GeeksforGeeks JavaScript

Using template literals

The template literals can also be used to add character to the string by storing the character into a variable and insert it at any position in the string using the template literal syntax.

NOTE: It allows you to add character at any position in the string.

Syntax:

`initialString${extraCharacter}`;

Example: The below code illustrates the use of the template literals to add a character to the string in JavaScript.

Javascript




const extraChar1 = 'f';
const str1 = `Geeks${extraChar1}orGeeks`;
const extraChar2 = 'S';
const str2 = `Java${extraChar2}cript`;
console.log(str1, str2);


Output

GeeksforGeeks JavaScript

Using slice() method

The slice() method can be used to add a character to a string by removing the string after the index where you want to add the character and then again use the slice() method with the original string to get back the removed string.

Syntax:

initialStr.slice(startInd, endInd) + 'extraCharacter' + initialStr.slice(indexAfterWhichTheStringWasRemoved);

Example: The below code is a practical implementation of the slice() method to add charater to a string.

Javascript




const str1 = "GeeksfoGeeks"
const updatedStr1 = str1.slice(0, 7) + 'r' + str1.slice(7);
const str2 = "JavaScrpt"
const updatedStr2 = str2.slice(0, 7) + 'i' + str2.slice(7);
console.log(updatedStr1, updatedStr2);


Output

GeeksforGeeks JavaScript

Using substring() method

The substring() method of the string can also be used to add a character to a string in the same way we use the slice() method by removing and adding the string to add the character at specified position.

Syntax:

initialStr.substring(startInd, endInd) + 'extraCharacter' + 
initialStr.substring(indexAfterWhichTheStringWasRemoved);

Example: The below code implements the substring() method to add a character to the string.

Javascript




const str1 = "GeekforGeeks"
const updatedStr1 = str1.substring(0, 4) + 's' + str1.substring(4);
const str2 = "JaaScript"
const updatedStr2 = str2.substring(0, 2) + 'v' + str2.substring(2);
console.log(updatedStr1, updatedStr2);


Output

GeeksforGeeks JavaScript

Using substr() method

The substr() method can be used to split a string into a small sub string then add the character and after that get the removed substring from the original string to completer it.

Syntax:

initialStr.substr(startInd, endInd) + 'extraCharacter' + 
initialStr.substr(indexAfterWhichTheStringWasRemoved);

Example: The below code explains the use of the substr() method to add a character to the string in JavaScript.

Javascript




const str1 = "GeesforGeeks"
const updatedStr1 = str1.substr(0, 3) + 'k' + str1.substr(3);
const str2 = "JavaSript"
const updatedStr2 = str2.substr(0, 5) + 'c' + str2.substr(5);
console.log(updatedStr1, updatedStr2);


Output

GeeksforGeeks JavaScript


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads