There are two ways by which we can copy one Array to the other.
Let us look at the first way using the slice() method.
<html> <body> <script> var x = ["Mohan", "Kriti", "Salim"] var y = x.slice() document.write("The Copied Array is : [ ",y," ]") </script> </body> </html>
So, in the above code we have created a Array and initialised to the variable x.
x = ["Mohan", "Kriti", "Salim"]
Below is how the values are positioned in the Array,
Then we have used the slice() method that would create an exact copy of x. And assign it to y.
var y = x.slice()
And we get the below output,
The Copied Array is : [ Mohan,Kriti,Salim ]
Now, let us look at the second way of copying a Array using concat() method.
<html> <body> <script> var x = ["Mohan", "Kriti", "Salim"] var y = [].concat(x) document.write("The Copied Array is : [ ",y," ]") </script> </body> </html>
So, in the above code we have created a Array and initialised to the variable x.
x = ["Mohan", "Kriti", "Salim"]
Below is how the values are positioned in the Array,
Then we have used the concat() method to join the existing array with an empty array. And the new array y would contain the same elements as the array x.
var y = [].concat(x)
And we get the below output,
The Copied Array is [ Mohan,Kriti,Salim ]