Copy the first two array elements to the last two array elements:
const fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.copyWithin(2, 0);
Try it Yourself »
Copy the first two array elements to the third and fourth position:
const fruits = ["Banana", "Orange", "Apple", "Mango", "Kiwi"];
fruits.copyWithin(2, 0, 2);
Try it Yourself »
The copyWithin()
method copies array elements to another position in the array.
The copyWithin()
method overwrites the existing values.
The copyWithin()
method does not add items to the array.
array.copyWithin(
target, start, end)
Parameter | Description |
target | Required. The index (position) to copy the elements to. |
start | Optional. The start index (position). Default is 0. |
end | Optional. The end index (position). Default is the array length. |
Type | Description |
Array | The changed array. |
copyWithin()
is an ECMAScript6 (ES6) feature.
ES6 (JavaScript 2015) is supported in all modern browsers:
Chrome | Edge | Firefox | Safari | Opera |
Yes | Yes | Yes | Yes | Yes |
copyWithin()
is not supported in Internet Explorer 11 (or earlier).
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!