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.
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//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
1var totn_string = '';
2
3console.log(totn_string.concat('Tech','On','The','Net'));
4
5//The following will be output to the web browser console log:
6
7TechOnTheNet
1// the fastest way to string concat in cycle when number of string is less than 1e6
2// see https://medium.com/@devchache/the-performance-of-javascript-string-concat-e52466ca2b3a
3// see https://www.javaer101.com/en/article/2631962.html
4function concat(arr) {
5 let str = '';
6 for (let i = 0; i < arr.length; i++) {
7 str += arr[i];
8 }
9
10 return str;
11}
12