Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

JavaScript copyWithin() Method

JavaScript copyWithin() Method

Array Object Reference 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

)

;

fruits Output result:

Banana,Orange,Banana,Orange

Try it »


Definition and Usage

The copyWithin() method copies 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 this method.

Method
copyWithin() 45.0 12.0 32.0 9 32.0

Syntax


array.copyWithin(target, start, end)

Parameters

Parameters Description
target Required. The target index position to copy to.
start Optional. The starting position for copying elements.
end Optional. The index position at which to stop copying (default is array.length). If negative, it counts backwards from the end.

Technical Details

Return value: arrays
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 result:

Banana,Orange,Banana,Orange,Kiwi,Papaya

Try it »


Array Object Reference JavaScript Array object