The concat() Function is used to create a new array by concatenating two or more arrays.
Let us say, we have two array,
["Mohan", "Kriti", "Salim"]
And
["Sia", "Andrew"]
And we want to concatenate them into one Array.
<html> <body> <script> var x = ["Mohan", "Kriti", "Salim"] var y = ["Sia", "Andrew"] var z = x.concat(y) document.write(z) </script> </body> </html>
So, in the above code we have created a Array and initialised to the variable x.
var x = ["Mohan", "Kriti", "Salim"]
Below is how the values are positioned in the Array,
Also we have another Array that contains, Sia and Andrew.
var y = ["Sia", "Andrew"]
Next, we have used the extend() function to add the new Array y that contains Sia and Andrew at the end of the Array x.
var z = x.concat(y)
And the Array y is joined with x.
And we get the below output,
Mohan,Kriti,Salim,Sia,Andrew
Now, what if we want to join three arrays.
Let us see in the below example.
<html> <body> <script> var x = ["Mohan", "Kriti", "Salim"] var y = ["Sia", "Andrew"] var z = ["Sam", "Cas"] var k = x.concat(y, z) document.write(k) </script> </body> </html>