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
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.
Syntax:
const stringFormation = (a, b, c) => {
return a + b + c;
};
Example: This example shows the use of the above-explained approach.
const stringFormation = (
inputPrefix,
inputString,
inputSuffix
) => {
return (
inputPrefix +
inputString +
inputSuffix
);
};
let testCase1 = "Geeks";
let testCase2 = " for ";
let testCase3 = "Geeks";
console.log(
stringFormation(
testCase1,
testCase2,
testCase3
)
);
Output
Geeks for Geeks
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.
Syntax
const stringFormation = (a, b, c) => {
return [a, b, c].join("");
};
Example: This example shows the use of the above-explained approach.
const stringFormation = (
inputPrefix,
inputString,
inputSuffix
) => {
return [
inputPrefix,
inputString,
inputSuffix,
].join("");
};
let testCase1 = "Geeks";
let testCase2 = " for ";
let testCase3 = "Geeks";
console.log(
stringFormation(
testCase1,
testCase2,
testCase3
)
);
Output
Geeks for Geeks
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.
Example: In this example we defines a function stringFormation that concatenates three input strings. It then tests the function with three test cases and prints the result.
const stringFormation = (
inputPrefix,
inputString,
inputSuffix
) => {
return [inputPrefix, inputString, inputSuffix].join('');
};
let testCase1 = "Geeks";
let testCase2 = " for ";
let testCase3 = "Geeks";
console.log(
stringFormation(
testCase1,
testCase2,
testCase3
)
);
Output
Geeks for Geeks
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.
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
Approach 5: Using the concat Method
In this approach, we use the concat method of the String object to concatenate the prefix, string, and suffix into a single string. The concat method provides a straightforward way to combine multiple strings.
Example:
const stringFormation = (prefix, string, suffix) => {
return prefix.concat(string, suffix);
};
console.log(stringFormation("pre", "fix", "suffix"));
console.log(stringFormation("Hello, ", "World", "!"));
console.log(stringFormation("Start-", "Middle", "-End"));
Output
prefixsuffix Hello, World! Start-Middle-End