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:

JavaScript
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)

Similar Reads

Approach 1: Using String Concatenation

In this approach, we are using string concatenation to combine multiple strings. We took three strings as input: prefix, string, and suffix, and then we returned the concatenated form of these strings....

Approach 2: Using Array Spread Operator

In this approach we are using Array Spread Operator. We took three strings as input: prefix, string, and suffix, and then we are forming a string by joining them through join() method....

Approach 4: Using Array Join

In this Approach we use the Array.join() method to concatenate the prefix, string, and suffix into a single string. By passing an array containing these elements to join(”), they are joined together without any separator, forming the desired result....

Approach 4: Using Template Literals (Template Strings)

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....