JavaScript copyWithin()Method

Array 对象参考手册JavaScript Array Object

Example

Copy the first two elements of the array to the last two elements:

var fruits = ["Banana", "Orange", "Apple", "Mango"]; fruits.copyWithin(2, 0);

fruitsOutput:

Banana,Orange,Banana,Orange

Try it Yourself »

Definition and Usage

The copyWithin() method copies elements from a specified position in an array to another specified position in the array.


Browser Support

The numbers in the table specify the first browser version that fully supports the method.

Method
copyWithin() 45.0 12.0 32.0 9 32.0

Syntax

array.copyWithin(target, start, end)

Parameters

Parameter Description
target Required. Copy to the specified target index position.
start Optional. The starting position of element copying.
end Optional. The index position to stop copying (default isarray.length). If negative, it means counting from the end.

Technical Details

Return Value: Array
JavaScript Version: ECMAScript 6

More Examples

Example

Copy the first two elements of the array to the third and fourth positions:

var fruits = ["Banana", "Orange", "Apple", "Mango", "Kiwi", "Papaya"]; fruits.copyWithin(2, 0, 2);

Output:

Banana,Orange,Banana,Orange,Kiwi,Papaya

Try it Yourself »

Array 对象参考手册JavaScript Array Object

Other Extensions