1var str1 = "Hello ";
2var str2 = "world!";
3var res = str1.concat(str2);
4console.log(res);
1
2var str1 = "Hello ";
3var str2 = "world!";
4var res = str1.concat(str2);
5// does not change the existing strings, but
6// returns a new string containing the text
7// of the joined strings.
1//This method adds two or more strings and returns a new single string.
2
3let str1 = new String( "This is string one" );
4let str2 = new String( "This is string two" );
5let str3 = str1.concat(str2.toString());
6console.log("str1 + str2 : "+str3)
7
8output:
9str1 + str2 : This is string oneThis is string two
1const str1 = 'Hello';
2const str2 = 'World';
3
4console.log(str1.concat(' ', str2));
5// expected output: "Hello World"
6
7console.log(str2.concat(', ', str1));
8// expected output: "World, Hello"
1
2let chocholates = ['Mars', 'BarOne', 'Tex'];
3let chips = ['Doritos', 'Lays', 'Simba'];
4let sweets = ['JellyTots'];
5
6let snacks = [];
7snacks.concat(chocholates);
8// snacks will now be ['Mars', 'BarOne', 'Tex']
9// Similarly if we left the snacks array empty and used the following
10snacks.concat(chocolates, chips, sweets);
11//This would return the snacks array with all other arrays combined together
12// ['Mars', 'BarOne', 'Tex', 'Doritos', 'Lays', 'Simba', 'JellyTots']
13
1It is strongly recommended to use the string
2concatenationoperators (+, +=) instead of String.concat
3method for perfomance reasons