How do I concatenate strings in JavaScript?

To concatenate strings in JavaScript, you can use the "+" operator or the string.concat(str1, str2, ...) method. The "+" operator is used to create a new string or modify (mutations) an existing line using the "+=" operator. The concat() method takes one or more strings as parameters and creates a new string. The method does not modify the provided strings but creates and returns a new string as the result of concatenating the passed strings. You can also use the array.join(separator) method for string concatenation, which joins all the elements of an array and returns a new string. The join() method takes a delimiter as a parameter (the default is a comma ","). In this JavaScript String Concatenate example, we use the "+" operator. More examples of string concatenation are given below. Click Execute to run the JavaScript String Concatenation Example online and see the result.
How do I concatenate strings in JavaScript? Execute
let str = 'JavaScript';
str += ' Concatenation ' + 'Example'

console.log(str);
Updated: Viewed: 1131 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