Share on Google+Share on Google+

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



Posted on: April 17, 2011 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Discuss: JavaScript Copy Array  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image