![]() |
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: Table of Content Using ‘+’ operatorThe + 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.
Output GeeksforGeeks JavaScript Using the concat() methodThe 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.
Output GeeksforGeeks JavaScript Using template literalsThe 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.
Output GeeksforGeeks JavaScript Using slice() methodThe 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.
Output GeeksforGeeks JavaScript Using substring() methodThe 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' + Example: The below code implements the substring() method to add a character to the string.
Output GeeksforGeeks JavaScript Using substr() methodThe 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' + Example: The below code explains the use of the substr() method to add a character to the string in JavaScript.
Output GeeksforGeeks JavaScript Using Array.prototype.join() MethodThe Array.prototype.join() method can be utilized to add a character to a string. By converting the string to an array of characters, inserting the new character at the desired position, and then joining the array back into a string, you can achieve this task. NOTE: This method allows you to add characters at any position in the string. Example: The below code demonstrates the use of the Array.prototype.join() method to add a character to a string in JavaScript.
Output hello- world Using Array.prototype.splice() MethodIn this approach, we utilize the Steps:
Example:This example demonstrates how to add a character to a string using the
Output Hello World Java-Script Using padEnd() MethodsJavaScript provides padEnd() and padStart() methods to pad the current string with another string until the resulting string reaches the given length. These methods can also be used to add characters to the beginning or the end of a string. Example: The padEnd() method pads the current string with a given string (repeated, if needed) until the resulting string reaches the specified length.
Output Hello! |
Reffered: https://www.geeksforgeeks.org
JavaScript |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 12 |