Home >>JavaScript Array Reference >JavaScript Array copyWithin() Method

JavaScript Array copyWithin() Method

JavaScript Array copyWithin() Method

The copyWithin() method is a mutable method and copies the part of an array to another location in the same array and returns without modifying its size. It does not change the length of the modified array.

Syntax:
array.copyWithin(target, start, end)

Parameter Values

Parameter Description
target It required index position to copy the elements to
start It is Optional. The index position to start copying elements from (default is 0)
end It is Optional.. The index position to stop copying elements from (default is array.length)

Browser Support

Method Chrome Edge Firefox Safari Opera
copyWithin() 45.0 12.0 32.0 9 32.0
Here is an example of JavaScript Array copyWithin() Method:
<html>
<body>
<button onclick="myCopy()">Try it</button>
<p id="copy"></p>
<script>
var fruits = ["Red", "Green", "Pen", "Book"];
document.getElementById("copy").innerHTML = fruits;
function myCopy() 
{  
  document.getElementById("copy").innerHTML = fruits.copyWithin(2,0);
}
</script>
</body>
</html>
Output:

Example 2:
<html>
<body>
<script>
var arr=["Bootstrap","AngularJS","Node.js","JQuery"];
var res=arr.copyWithin(2);
document.writeln(res);
</script>
</body>
</html>
Output:

No Sidebar ads