String Concatenation in JavaScript

To concatenate strings in JavaScript, you can use the "+" operator or the string.concat(str1, str2, ...) method. The "+" operator creates a new string by concatenating strings to the left and right of the operator. The string.concat() method takes one or more strings and concatenates them into a new string. The string.concat() method does not modify the provided strings but creates and returns a new string resulting from the concatenation. In this JavaScript String Concatenate example, we use the string.concat() method to concatenate strings. More examples of string concatenation are given below. Click Execute to run the JavaScript String Concatenation Example online and see the result.
String Concatenation in JavaScript Execute
const str = 'JavaScript '.concat('Concatenation ', 'Example');

console.log(str);
Updated: Viewed: 4450 times

How to concatenate two strings in JavaScript?

The most comfortable way to concatenate two strings in JavaScript is to use the "+" operator. The operator concatenates or glues strings together. Write two strings and a "+" sign between them to do this.

Concatenate strings with "+" operator
let str1 = 'Hello';
let str2 = 'World';

console.log(str1 + str2);

// output: HelloWorld

Note that the "+" operator does not add anything between or after these variables. Therefore, to make the text more readable, we need to use the "+" operator again to add a space.

Concatenate strings with a separator between them
let str1 = 'Hello';
let str2 = 'World';

console.log(str1 + ' ' + str2);

// output: Hello World

You can also use a mutative approach using the "+=" operator

Concatenate strings with "+=" operator
let str = 'JavaScript';
str += ' Concatenation ' + 'Example'

console.log(str);

// output: JavaScript Concatenation Example

Concatenating JavaScript String using the concat() method

JavaScript has a built-in string.concat(str1, str2, ...) method that concatenates two or more strings. The concat() method does not change the provided strings but returns a new string containing the concatenating the strings. The concat() method is rarely used because it creates more bugs and unexpected behavior than the "+" operator.

JavaScript concat() Syntax
string.concat(str1, str2, ..., strX)

Where:
  • str1, str2, strX: the strings to be connected
JavaScript concat() Example
let str1 = 'JavaScript';
let str2 = 'Concatenation';
let str3 = 'Example';

console.log(str1.concat(' ', str2, ' ', str3));

// output: JavaScript Concatenation Example

How to concatenate array elements into a string in JavaScript?

To concatenate the elements of an array into a string in JavaScript, you can use the array.join(separator). This method concatenates the elements of an array and returns a string. The method takes a delimiter as a parameter. If no delimiter is specified, a comma "," is used by default.

JavaScript join() Syntax
array.join(separator)

Where:
  • separator (optional): the delimiter to be used. The default is a comma (,).
JavaScript Concatenate Array Elements into a String Example
const arr = ['Washington', 'London', 'Madrid'];

console.log(arr.join(' '));

// output: Washington London Madrid

See also