JavaScript Array concat()
Syntax & Examples
Array.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 Array.concat()
There are 4 variations for the syntax of Array.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.
✐ 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,
- We define a string variable
strwith the value'Hello'. - We use the
concat()method with no arguments. - The result is stored in the variable
newStr. - We log
newStrto the console using theconsole.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,
- We define a string variable
strwith the value'Hello'. - We use the
concat()method with the argument' World'. - The result is stored in the variable
newStr. - We log
newStrto the console using theconsole.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,
- We define a string variable
strwith the value'Hello'. - We use the
concat()method with the arguments' and'and' welcome'. - The result is stored in the variable
newStr. - We log
newStrto the console using theconsole.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 Array: the syntax and few working examples with output and detailed explanation for each example.