Javascript examples for Array:copyWithin
The copyWithin() method copies array elements within the array, to and from specified positions.
Parameter | Description |
---|---|
target | Required. The index position to copy the elements to |
start | Optional. The index position to start copying elements from? (default is 0) |
end | Optional. The index position to stop copying elements from (default is array.length) |
An Array, the changed array
The following code shows how to Copy the first two array elements to the last two array elements:
<!DOCTYPE html> <html> <body> <button onclick="myFunction()">Test</button> <p id="demo"></p> <script> var fruits = ["a","b","c","d","e"]; document.getElementById("demo").innerHTML = fruits; function myFunction() {/*from www.j av a2s .c o m*/ document.getElementById("demo").innerHTML = fruits.copyWithin(2,0,2); } </script> </body> </html>