JavaScript Code

String concat() in JavaScript


String concat()

In JavaScript, String.concat() method is used to concatenate the string with the given string(s).

Syntax

str1.concat(str2)
str1.concat(str2, str3)
str1.concat(str2, str3, str4, .., strN)

where

  • str1, str2, str3,.. strN are strings.

concat() method takes one or more strings as arguments, concatenates them with the calling string, and returns the resulting string. Original string str is not modified.

Examples

1. In the following program, we concatenate the value in string str1 with the string value in str2.

let str1 = 'apple';
let str2 = 'banana';
let output = str1.concat(str2);
console.log(output);

2. In the following program, we concatenate the value in string str1 with two string values in str2 and str3.

let str1 = 'apple';
let str2 = 'banana';
let str3 = 'cherry';
let output = str1.concat(str2, str3);
console.log(output);

Copyright @2022