The concat() method is used to join two or more arrays by returning a new array containing all the elements. The syntax would be as below:
1 | array1.concat(array2, array3, ..., arrayX) |
Let’s take an example of array’s concatenation with veggies and fruits arrays,
1 2 3 4 5 | var veggies = ["Tomato", "Carrot", "Cabbage"]; var fruits = ["Apple", "Orange", "Pears"]; var veggiesAndFruits = veggies.concat(fruits); console.log(veggiesAndFruits); // Tomato, Carrot, Cabbage, Apple, Orange, Pears |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.