Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Java Javascript-array JavaScript Copy Array

Related Tutorials


 
 

Share on Google+Share on Google+

JavaScript Copy Array

Advertisement
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..

<html>
<head>
<h2>Copy Array Example</h2>
<script language="javascript">
var arr1 = new Array();
arr1[0]="Rose";
arr1[1]="India";
arr1[2]="Technologies";

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>");
}
</script>
</head>
<body bgcolor="lightBlue">
</body>
</body>
</html>

Output will be displayed as:

Download Source Code

 

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  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.

Posted on: April 17, 2011

Related Tutorials

Discuss: JavaScript Copy Array  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com