JavaScript String concat()
Syntax & Examples


String.concat() method

The concat() method of the String class in JavaScript combines the text of two (or more) strings and returns a new string.


Syntax of String.concat()

There are 4 variations for the syntax of String.concat() method. They are:

1.
concat()

This method returns the original string since no arguments are provided.

Returns value of type String.

2.
concat(str1)

Parameters

ParameterOptional/RequiredDescription
str1optionalThe first string to concatenate.

This method returns a new string consisting of the original string followed by str1.

Returns value of type String.

3.
concat(str1, str2)

Parameters

ParameterOptional/RequiredDescription
str1optionalThe first string to concatenate.
str2optionalThe second string to concatenate.

This method returns a new string consisting of the original string followed by str1 and str2.

Returns value of type String.

4.
concat(str1, str2, /* …, */ strN)

Parameters

ParameterOptional/RequiredDescription
str1optionalThe first string to concatenate.
str2optionalThe second string to concatenate.
strNoptionalAdditional strings to concatenate.

This method returns a new string consisting of the original string followed by str1, str2, and additional strings up to strN.

Returns value of type String.



✐ Examples

1 Using concat() method with no arguments

In JavaScript, if no arguments are provided to the concat() method, it returns the original string.

For example,

  1. We define a string variable str with the value 'Hello'.
  2. We use the concat() method with no arguments.
  3. The result is stored in the variable newStr.
  4. We log newStr to the console using the console.log() method.

JavaScript Program

const str = 'Hello';
const newStr = str.concat();
console.log(newStr);

Output

Hello

2 Using concat() method with one argument

In JavaScript, we can use the concat() method to concatenate one string to the original string.

For example,

  1. We define a string variable str with the value 'Hello'.
  2. We use the concat() method with the argument ' World'.
  3. The result is stored in the variable newStr.
  4. We log newStr to the console using the console.log() method.

JavaScript Program

const str = 'Hello';
const newStr = str.concat(' World');
console.log(newStr);

Output

Hello World

3 Using concat() method with multiple arguments

In JavaScript, we can use the concat() method to concatenate multiple strings to the original string.

For example,

  1. We define a string variable str with the value 'Hello'.
  2. We use the concat() method with the arguments ' and' and ' welcome'.
  3. The result is stored in the variable newStr.
  4. We log newStr to the console using the console.log() method.

JavaScript Program

const str = 'Hello';
const newStr = str.concat(' and', ' welcome');
console.log(newStr);

Output

Hello and welcome

Summary

In this JavaScript tutorial, we learned about concat() method of String: the syntax and few working examples with output and detailed explanation for each example.