JavaScript concat

Usage

var str1 = "Hello ";
var str2 = "Reader!";
var result = str1.concat(str2);  //  string.concat(string1, string2, ..., stringN)

// result
console.log(result);   // Output - Hello Reader!

Parameters

  • string: You can pass any number of strings to concatenate, atleast one string param is required.

Return value

  • Returns new string generated by concatenating all string params.

The concat() method joins two or more strings and return a new string.

The concat() method does not change existing string.

One string param is required atleast.

Syntax

string.concat(string1, string2, ..., stringN)

Parameters

string: A string parameter is required for concat(). To concat more than two strings, you can pass N number of string param, each separated by comma.

Example

concat

var myString = "String to concat";
var param2 = "abc";

console.log(myString.concat(" with one param"));  // Output: String to concat with one param
console.log(myString.concat(" with one param ",param2)); // Output: String to concat with one param abc

console.log("".concat(4,5));  // Output: 45
console.log("4".concat(5));  // Output: 45

concat an array

var greetList = ['Hello', ' ', 'Pankaj', ' ', 'Anupam', '!'];

console.log( "".concat(...greetList) );  // Output: Hello Pankaj Anupam!
console.log( "".concat(greetList) );    // Output: Hello, ,Pankaj, ,Anupam,!

Tip

It is recommended that use assignment operators (+, +=) instead of the concat() method for better performance.