JavaScript String concat() Method JavaScript String concat() Method

The Javascript concat method is a great function and this article will hopefully answer your burning question of how do you concatenate strings in javascript?

Table of contents:

Let's dive right in to show, for example, to concatenate strings and returns a new string.

String Concatenation in JavaScript

When you want to concatenate strings in javascript you can also use the string concat method two concatenate two or more strings.

It is important to note that the concat method returns a new string and does not update any of the strings being concatenated. Your next burning question is probably: What is string concatenation with example? so let's dive into some real-world examples.

Using the concat() method

Let's look at an example of javascript string concatenation with the following code to concatenate strings. To use the concat method for example:

var text1 = "Hello";
var text2 = "world!";
var result = text1.concat(" ", text2);

In the following example you can see that I am actually not just doing string concatenation with two strings, but three strings because I want to add a space between the two strings.

The Javascript concat method format is very similar to how an array is created with a comma delimited list of a string. Because this is Javascript, if you're question is How do I concatenate strings in node JS? The answer is use these same approaches and it will work perfectly.

Using the + operator

To concatenate a new string it is not required to use the concat method when working with javascript string. In stead you can also just use the + operator to concatenate the strings together.

This method is a great example to answer the question of Which operator does JavaScript use for concatenation? OR Can you add strings in JavaScript? As you will see that the operator Javascript uses is the + operator.

For example, to concatenate a javascript string you can do the following without the concat method to a string:

var text1 = "Hello";
var text2 = "world!";
var result = text1 + " " + text2;

The end result in this example, the string of the results in Javascript will be identical regardless of the approach used for the Javascript string.

Using the Javascript array join() method

The final approach to concatenate strings with javascript is to leverage the array join() method. This approach is quite similar to the concat method while still being a little bit different. For a very similar example using the reduce function you can see how I accomplished to group an array of objects by key with Javascript.

Let's look at the final example of how to concatenate the string without the string concat method.

var texts = ["Hello", "World!"];
var result = texts.join(" ");

The array join method uses an array instead of multiple variables containing just a string. The value inside the join function can be any value you'd like. Perhaps a slightly more practical example would be to concatenate a string separate by a comma:

var cars = ["Mazda", "Audi", "Tesla", "Nissan"];
var result = cars.join(", ");

In this example I am doing string concatenation with a comma and a space character. Ironically the results will be nearly identical to how I defined the cars array with the exception there will be no double quotes between the list of cars.

Published on Jul 20, 2022

Tags: concat | JavaScript | Javascript Arrays Tutorial

Related Posts

Did you enjoy this article? If you did here are some more articles that I thought you will enjoy as they are very similar to the article that you just finished reading.


Learn how to code in HTML, CSS, JavaScript, Python, Ruby, PHP, Java, C#, SQL, and more.

No matter the programming language you're looking to learn, I've hopefully compiled an incredible set of tutorials for you to learn; whether you are beginner or an expert, there is something for everyone to learn. Each topic I go in-depth and provide many examples throughout. I can't wait for you to dig in and improve your skillset with any of the tutorials below.