var str1 = "Hello ";
var str2 = "world!";
var res = str1.concat(str2);
// does not change the existing strings, but
// returns a new string containing the text
// of the joined strings.
//This method adds two or more strings and returns a new single string.
let str1 = new String( "This is string one" );
let str2 = new String( "This is string two" );
let str3 = str1.concat(str2.toString());
console.log("str1 + str2 : "+str3)
output:
str1 + str2 : This is string oneThis is string two
/*
Note:
Concatenation in javascript
Concat() The concat() method is used to merge two or more arrays.
This method does not change the existing arrays but instead returns a new array.
Concatenation is when you add string to an integer then the integer becomes a string.
*/
//CODE//
var myAge = 100;
console.log("My age is" + myAge);
myAge = 29;
console.log("My age next year will be" + myAge);
let first_string = 'I am a programmer, ';
let second_string = 'I am indisposable.';
let what_i_said = first_string + second_string;
console.log(what_i_said);
/* OR
*/
let tell_my_boss = first_string.concat(second_string);
console.log(tell_my_boss);
// the fastest way to string concat in cycle when number of string is less than 1e6
// see https://medium.com/@devchache/the-performance-of-javascript-string-concat-e52466ca2b3a
// see https://www.javaer101.com/en/article/2631962.html
function concat(arr) {
let str = '';
for (let i = 0; i < arr.length; i++) {
str += arr[i];
}
return str;
}
//ES6 spread operator same as arr1.concat(arr2)
var arr1 = ['1', '2', '3']
var arr2 = [4, 5, 6]
arr1 = [...arr1, arr2] //... removes the [ and ]
// leavin only an instance of inside.
output:
['1', '2', '3', 4, 5, 6 ]