JavaScript String concat() Method Tutorial

JavaScript provides the concat() method in order to concatenate the string arguments and return a new string. The concat() method is similar to the Python echo() method.

concat() Method Syntax

The concat() method is provided with string objects where provided string parameters will be added to the string object and returned as a new string.

  • STRING is a string object which contains a string value. The STRING object value will not change after the execution of the concat() method.
  • STR1, STR2, and STRN are string values or object which will be added to the STRING object value. The concat() method can accept single or multiple string values or objects.

Concatante Two Strings with concat() Method

The concat() method can accept single or multiple parameters like below. We will provides string value or string objects in order to add the str1 and str2 string objects and return new string.

const str1 = 'Hello';
const str2 = 'WiseTut';

console.log(str1.concat(' '));
// expected output: "Hello World"

console.log(str2.concat(', ', str2 ,' This is another string'));
// expected output: "World, Hello"

let hello = 'Hello, '
console.log(hello.concat('WiseTut', '. Have a nice day.'))
// Hello, Kevin. Have a nice day.

let greetList = ['Hello', ' ', 'Venkat', '!']
console.log("".concat(...greetList))  //

"".concat(5, 9)  // "59"
"".concat([])    // ""
"".concat({})    // [object Object]
"".concat(null)  // "null"
"".concat(false)  // "false"

The output will be like below which will be printed to the console.

 "Hello " 
 "WiseTut, WiseTut This is another string"
 "Hello, WiseTut. Have a nice day." 
"Hello Venkat!"

concat() Method Alternative + Operator

Even the concat() command adds multiple strings together it has some performance issues for heavy usage. Alternatively + operator can be used to concat multiple strings together.

console.log("Hello" + " WiseTut")

console.log("Hello" + " WiseTut" + "This is LinuxTect")

The output will be like below.

 "Hello WiseTut" 
"Hello WiseTutThis is LinuxTect"

Leave a Comment