JavaScript Copy Array

This page discusses - JavaScript Copy Array

JavaScript Copy Array


The feature of coping the data of one array into another is very useful in complex applications. In this section, you will study how to copy an array. You can see in the given example, we have created an array object 'arr1' consisting of elements 'Rose', 'India', and 'Technologies'. Then we have created another array object 'arr2' and by using the method slice(), the elements of arr1 is copied to the arr2. 

Code for copying the value of one array into another using JavaScript..

<h2>Copy Array Example</h2>
<script language="javascript">
var arr1 = new Array();

var arr2 = arr1.slice();
document.write("<b>Array is: </b><br>");
for (var i = 0; i < arr1.length; i++){
document.write(arr1[i] + "<BR>");
document.write("<BR><b>Copied Array is: </b><br>");
for (var i = 0; i < arr2.length; i++){
document.write(arr2[i] + "<BR>");
<body bgcolor="lightBlue">

Output will be displayed as:

Download Source Code