JavaScript Array copyWithin() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Array

Definition

Array.prototype.copyWithin(target, start, end) copies a slice of the array onto another position in the same array. It mutates in place, keeps the same length, and returns the array.

Remember
array.copyWithin(target, start, end?)

target → where writing begins
start  → first source index (inclusive)
end    → stop index (exclusive); default = length

mutates the array  ·  length unchanged
negative indexes count from the end

Syntax

Call array.copyWithin. Start with this line.

JavaScript
arr.copyWithin(target, start, end?)
CallWhat it does
arr.copyWithin(target, start, end?)Primary signature.
array.copyWithin(...)Call with the arguments your case needs.

Built into JavaScript. Call copyWithin(...) on an array instance — no import required.

Library declarations
JavaScript
arr.copyWithin(target, start, end?)

Parameters

ParameterDescription
targetRequired. Write start index
startRequired. Source start (*defaults to 0 if omitted)
endOptional. Source stop (exclusive); default = length

Return Value

Return ValueDescription
*The same array

JavaScript Examples

Example 1 — Copy a Slice to the Front

Copy indexes 3–5 and paste them starting at index 0.

JavaScript
const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9];

numbers.copyWithin(0, 3, 6);

console.log(numbers);
// [4, 5, 6, 4, 5, 6, 7, 8, 9]
Try It Yourself

How It Works

  1. Pick the source. Indexes 3, 4, 5 hold 4, 5, 6 (end is exclusive).
  2. Pick the target. Writing starts at index 0.
  3. Overwrite three slots. Positions 0–2 become 4, 5, 6.
  4. Length stays 9. The rest of the array is left as-is after the paste window.

Example 2 — Negative start from the Tail

Copy the last three elements to the front without writing length - 3.

JavaScript
const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9];

numbers.copyWithin(0, -3);

console.log(numbers);
// [7, 8, 9, 4, 5, 6, 7, 8, 9]
Try It Yourself

How It Works

  1. Resolve -3. On length 9, that is index 6.
  2. Omit end. Source runs through the last element → 7, 8, 9.
  3. Paste at 0. The front becomes 7, 8, 9.
  4. Overlap is OK. Tail values can appear twice after the copy.

Example 3 — Duplicate a Pattern

Copy the first three elements into the next three slots.

JavaScript
const pattern = [1, 2, 3, 0, 0, 0];

pattern.copyWithin(3, 0, 3);

console.log(pattern);
// [1, 2, 3, 1, 2, 3]
Try It Yourself

How It Works

  1. Source window. Indexes 0–2 hold 1, 2, 3.
  2. Target window. Writing starts at index 3.
  3. Overwrite zeros. Slots 3–5 become 1, 2, 3.
  4. Handy pattern. Good for tiling a small template in a fixed buffer.

Browser & Runtime Support

Array.prototype.copyWithin() was added in ES2015 (ES6). It is available in all modern browsers and Node.js.

✓ Baseline · ES2015

Array.prototype.copyWithin()

Supported in Chrome 45+, Firefox 32+, Safari 9+, Edge 12+, and Node.js 4+. Not available in Internet Explorer.

97% Modern browser support
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full support
Array.copyWithin() Excellent

Bottom line: Safe for modern apps. For IE11, use a polyfill or a manual loop that copies into a temporary buffer first.

Frequently Asked Questions

copyWithin() copies a sequence of elements from one part of an array to another within the same array, overwriting existing slots. It returns the modified array and does not change length.
Yes. It modifies the array in place. The return value is the same array reference, so you can chain calls.
target is where writing begins. start is the first source index (inclusive). end is exclusive—elements from start up to but not including end are copied. Negative indexes count from the end.
Copying continues through the last element of the array.
slice() returns a new shallow copy and leaves the original alone. copyWithin() writes inside the existing array and mutates it.
copyWithin() is ES2015 (ES6). All modern browsers and Node.js support it. Internet Explorer does not.

Next: entries()

Learn how to iterate index/value pairs with Array.entries().

entries() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful