Concat
Array
The concat()
method is used to merge two or more arrays. This method doesn't mutate the initial arrays, instead it returns a new one.
const fruits1 = ['apple', 'pear', 'grape'];
const fruits2 = ['banana', 'orange', 'kiwi'];
const fruits = fruits1.concat(fruits2);
console.log(fruits); //expected output: ['apple', 'pear', 'grape', 'banana', 'orange', 'kiwi'];
String
The concat()
method concatenates the string arguments to the calling string and returns a new string.
let str1 = 'My favorite fruits are: ';
let str2 = 'apple, kiwi and banana';
console.log(str1.concat(str2)); //expected output: 'My favorite fruits are: apple, kiwi and banana'
MDN: Array.prototype.concat()
MDN: String.prototype.concat()