Easy Tutorial
❮ Dom Obj Label Dom Obj Quote ❯

JavaScript copyWithin() Method

JavaScript Array Object

Example

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

fruits output result:

Banana,Orange,Banana,Orange

Definition and Usage

The copyWithin() method is used to copy elements from a specified position in the array to another specified position in the array.


Browser Support

The numbers in the table indicate the first browser version that 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. The target index position to copy to.
start Optional. The starting position for copying elements.
end Optional. The index position to stop copying (default is array.length). If negative, it indicates 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:

Output result:

Banana,Orange,Banana,Orange,Kiwi,Papaya

JavaScript Array Object

❮ Dom Obj Label Dom Obj Quote ❯