How to use template literals In Javascript
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.
const extraChar1 = 'f';
const str1 = `Geeks${extraChar1}orGeeks`;
const extraChar2 = 'S';
const str2 = `Java${extraChar2}cript`;
console.log(str1, str2);
Output
w3wiki JavaScript
Add Characters to a String in JavaScript
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 ‘+’ operator
- Using the concat() method
- Using template literals
- Using slice() method
- Using substring() method
- Using substr() method
- Using Array.prototype.join() Method