How to useTemplate Literals (Template Strings) in Javascript
In this approach, we use template literals (template strings) to concatenate the prefix, string, and suffix into a single string. Template literals allow for easy interpolation of variables and expressions within strings.
Syntax:
const stringFormation = (prefix, string, suffix) => {
return `${prefix}${string}${suffix}`;
};
Example:
const stringFormation = (prefix, string, suffix) => {
return `${prefix}${string}${suffix}`;
};
const prefix = "Geeks";
const suffix = "Geeks";
const string = " for ";
console.log(stringFormation(prefix, string, suffix));
Output
Geeks for Geeks
JavaScript Program to Add Prefix and Suffix to a String
In this article, we are going to implement a JavaScript program through which we can add a prefix and suffix to a given string. A prefix is added to the beginning of a word to modify its meaning. A suffix is added to the end of a word. Together, they form a prefix-suffix string, enabling the creation of new words or altering the existing word’s meaning or grammatical category.
Table of Content
- Approach 1: Using String Concatenation
- Approach 2: Using Array Spread Operator
- Approach 4: Using Array Join
- Approach 4: Using Template Literals (Template Strings)