Add a String to Beginning and End of another String in JS

avatar
Borislav Hadzhiev

Last updated: Mar 4, 2024
2 min

banner

# Add a String to the beginning and end of another String

Use the addition (+) operator to add a string to the beginning and end of another string, e.g. "before" + str + "after".

When used with strings, the addition operator concatenates the strings and returns the result.

index.js
const str = 'ello worl'; const result = 'h' + str + 'd'; // ๐Ÿ‘‡๏ธ "hello world" console.log(result);

add string to beginning and end of another string

The code for this article is available on GitHub

We used the addition (+) operator to add a string to the beginning and end of another string.

When used with strings, the addition operator concatenates them, and when used with numbers, the operator sums the numbers.

index.js
console.log('ab' + 'cd'); // ๐Ÿ‘‰๏ธ "abcd" console.log(2 + 2); // ๐Ÿ‘‰๏ธ 4

You could achieve the same result by using a template literal.

# Using a template literal

You can also use a template literal to add a string to the beginning and end of another string.

Template literals allow us to embed expressions into a string.

index.js
const str = 'ello worl'; const result = `h${str}d`; // ๐Ÿ‘‡๏ธ "hello world" console.log(result);

using a template literal

The code for this article is available on GitHub

We wrapped the string using backticks, which makes it a template literal.

The dollar sign and curly braces part ${} is an expression that gets evaluated.

In our case, the value of the str variable replaces the ${str} part of the template literal.

Alternatively, you can use the String.join() method.

# Using Array.join()

This is a two-step process:

  1. Wrap the strings in an array.
  2. Use the Array.join() method to join the strings without a separator.
index.js
const prefix = 'h'; const suffix = 'd'; const str = 'ello worl'; const result = [prefix, str, suffix].join(''); console.log(result); // ๐Ÿ‘‰๏ธ hello world

using array join to add string to beginning and end

The code for this article is available on GitHub

We wrapped the strings in an array to be able to use the Array.join() method.

The Array.join() method concatenates all of the elements in an array using a separator.

The only argument the Array.join() method takes is a separator - the string used to separate the elements of the array.

If the separator argument is set to an empty string, the array elements are joined without any characters in between them.

# Append a string to the end of another string

If you only need to append a string to another string, you can also use the String.concat() method.

The concat method concatenates the supplied parameters to the string.

index.js
const str = 'bobby '; const result = str.concat('hadz'); console.log(result); // ๐Ÿ‘‰๏ธ "bobby hadz"
The code for this article is available on GitHub

The String.concat() method takes one or more strings as parameters and concatenates them to the string on which it was called.

index.js
const result = 'one '.concat('two', ' three'); console.log(result); // ๐Ÿ‘‰๏ธ "one two three"

The method returns a new string containing the combined text.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev