Array copyWithin() Method - Javascript Array

Javascript examples for Array:copyWithin

Description

The copyWithin() method copies array elements within the array, to and from specified positions.

Parameter Values

Parameter Description
targetRequired. 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)

Return Value:

An Array, the changed array

The following code shows how to Copy the first two array elements to the last two array elements:

Demo Code

ResultView the demo in separate window

<!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>

Related Tutorials