The concat() method of the String class in JavaScript combines the text of two (or more) strings and returns a new string.
There are 4 variations for the syntax of String.concat() method. They are:
concat()
This method returns the original string since no arguments are provided.
Returns value of type String
.
concat(str1)
Parameters
Parameter | Optional/Required | Description |
---|---|---|
str1 | optional | The first string to concatenate. |
This method returns a new string consisting of the original string followed by str1.
Returns value of type String
.
concat(str1, str2)
Parameters
Parameter | Optional/Required | Description |
---|---|---|
str1 | optional | The first string to concatenate. |
str2 | optional | The 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
.
concat(str1, str2, /* …, */ strN)
Parameters
Parameter | Optional/Required | Description |
---|---|---|
str1 | optional | The first string to concatenate. |
str2 | optional | The second string to concatenate. |
strN | optional | Additional 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
.
In JavaScript, if no arguments are provided to the concat()
method, it returns the original string.
For example,
str
with the value 'Hello'
.concat()
method with no arguments.newStr
.newStr
to the console using the console.log()
method.const str = 'Hello';
const newStr = str.concat();
console.log(newStr);
Hello
In JavaScript, we can use the concat()
method to concatenate one string to the original string.
For example,
str
with the value 'Hello'
.concat()
method with the argument ' World'
.newStr
.newStr
to the console using the console.log()
method.const str = 'Hello';
const newStr = str.concat(' World');
console.log(newStr);
Hello World
In JavaScript, we can use the concat()
method to concatenate multiple strings to the original string.
For example,
str
with the value 'Hello'
.concat()
method with the arguments ' and'
and ' welcome'
.newStr
.newStr
to the console using the console.log()
method.const str = 'Hello';
const newStr = str.concat(' and', ' welcome');
console.log(newStr);
Hello and welcome
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.