Follow @endyourif rss Facebook LinkedIn

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

Published on Jul 20, 2022

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.

Tags: concat | JavaScript | Javascript Arrays Tutorial

About Jamie Munro

Welcome to my blog. I have been compiling articles since 2009! Below is a list of every post I've ever created. Some of my major focuses have been compiling the best source of jQuery tutorials on the Internet today. A recent focus of mine is now focusing on creating the best SQL Tutorials for Beginners, Intermediate and Advanced Users. This is a compiliation of all my experience about how to do common SQL commands to optimize for performance. As you might see from the links around here, I've also written and published 5 books. My first and last book are focused around ASP.NET MVC and Web API tutorials. The last book has a big focus on integrating Knockout.js as the MVVM for the front-end code. O'Reilly Media was so interested in Knockout.js that they actually commissioned me to write that book first as it is one of the only books dedicated entirely to this MVVM library. So if you're looking for the best Knockout.js tutorials, I've compiled the best list. My first book, even though it was self published, was on the PHP framework: CakePHP. I used this framework for years and many of my first ever exampes were all focused on CakePHP tutorials that provide so many fantastic PHP examples. There are so many Javascript tutorials out there, but I noticed that one of the things I work on most with Javascript is arrays. With this I've worked hard on compiling a big list of really advanced Javascript array examples. These array examples are amazing including how to group by, get distinct array elements and so many more. I've also dabbled a little bit with Node.js and during those experiments I definitely ran into some common errors like Can't set headers after they are sent, Solving No Access-Control-Allow-Origin with Node js and Express, and Uncaught ReferenceError: require is not defined. With these common errors I've compiled a list of Node.js tutorials that help solve these problems, but more importantly getting you started with the basics.