During JavaScript web application development, we may get a requirement to prepend a string to the beginning of another string. Read this article’s end to prepend a string to the beginning of another string in JavaScript.
Prepend a string to the beginning of another string in JavaScript
We have the following four solutions:
- Use the Template Literals
- Use the Addition Operator
- Use the replace() method
- Use the concat() method
Now, we will take a closer look at each solution with specific examples for each of them.
Use the Template Literals
Pattern characters are contained in backtick character pairs (`).
Template literals are commonly used for string interpolation (create strings by doing substitution of placeholders).
Syntax:
${...}
Example:
const lsi1 = 'IT';
// Prepend the string 'LearnShare' to the beginning of the string 'IT'
const lsi2 = `LearnShare${lsi1}`;
console.log(lsi2);
Output:
LearnShareIT
Click here if you want to learn more about template literals.
Use the Addition Operator
The addition operator (+) sums the operands if they are numbers or concatenates them if at least one operand is a string.
Example:
const lsi1 = 'IT';
// Prepend the string 'LearnShare' to the beginning of the string 'IT'
const lsi2 = 'LearnShare' + lsi1;
console.log(lsi2);
Output:
LearnShareIT
Use the replace() method
The replace() method returns a new string where the specified value(s) has been replaced.
Syntax:
string.replace(searchValue, newValue)
Parameters:
- searchValue: The value, or regular expression, to search for.
- newValue: The new value to replace.
We set the searchValue parameter to an empty string in the following example. Therefore, the replacement string is added to the beginning of the original string.
Example:
const lsi1 = 'IT';
// Prepend the string 'LearnShare' to the beginning of the string 'IT'
const lsi2 = lsi1.replace ('','LearnShare');
console.log(lsi2);
Output:
LearnShareIT
Use the concat() method
The concat() method returns a new string containing the concatenated strings.
Syntax:
string.concat(string1, string2, ..., stringX)
Parameters:
- string1, string2, …, stringX: Strings concatenated.
Example:
const lsi1 = 'IT';
// Prepend the string 'LearnShare' to the beginning of the string 'IT'
const lsi2 = "LearnShare".concat(lsi1);
console.log(lsi2);
Output:
LearnShareIT
Summary
This article has shown how to prepend a string to the beginning of another string in JavaScript. I hope the information in this article will be helpful to you. If you have any problems, please comment below. I will answer. Thank you for reading!
Maybe you are interested:
- Remove Substring From String in JavaScript
- Remove everything after specific Character in JavaScript
- Capitalize the First Letter of Each Word in JavaScript

Hello, my name’s Bruce Warren. You can call me Bruce. I’m interested in programming languages, so I am here to share my knowledge of programming languages with you, especially knowledge of C, C++, Java, JS, PHP.
Name of the university: KMA
Major: ATTT
Programming Languages: C, C++, Java, JS, PHP