How to combine two strings in JavaScript

Today, we’ll explain to you how to combine two strings in JavaScript.

We can assign a string to a variable and combine the variable with another string. Here, we will show you the different ways to join two or more strings in JavaScript.

Suppose, we have a first name and last name and want to join both names so we can join two strings and create a full name using JavaScript.

Different ways to concatenate strings in JavaScript

  1. Using + operator
  2. Template string
  3. concat() method
  4. join() method

1. Using + operator

We can use the + operator to create a new string or change the existing string by adding it.

In the above code, we have combined the strings which is assigned to the variable. Now, we will join the variable to another string.

Also, append the variable to an existing string using += as shown below.

2. Template string

It’s a common issue to missing space when concatenating strings using + operator. In JavaScript, you can add interpolate variables into string using template string as shown below.

3. concat() method

Using Javascript built-in concat() method, we can join two or more strings.

The concat() method does not change the existing strings and returns a new string that results from concatenates the string arguments to the original string.

Syntax

Example

4. join() method

At last, the join() method concatenates all elements of the array and return a string. The join() method accepts an optional parameter called separator that separates the elements of the string. The default separator value is comma (,).

Syntax

Example

I hope you find this article is helpful.
Thank you for reading. Happy Coding..!! 🙂

If you found value in this article,
you can support us by buying me a coffee! ☕

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *