JavaScript Array copyWithin() Method

Beginner
⏱️ 9 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
In-place copy

What You’ll Learn

The copyWithin() method copies a run of elements from one index range to another inside the same array, overwriting what was there. Length stays the same; only slot values change. This tutorial covers syntax, five examples, overlap behavior, and how it differs from slice and splice.

01

Syntax

copyWithin(t,s,e)

02

Target

Write start index

03

Start / End

Source range

04

In-place

Mutates array

05

Same length

No resize

06

Overlap OK

Safe overlap

Introduction

Sometimes you need to move or duplicate a block of values within one array—shift the last few items to the front, fill a gap with a repeated pattern, or rotate a buffer without allocating a new array. That is what copyWithin() is for.

Unlike concat(), which returns a new array, copyWithin() modifies the array you call it on. Unlike splice(), it never inserts or removes slots—the array length is unchanged.

Understanding the copyWithin() Method

Think of copyWithin(target, start, end) as selecting elements from index start up to (but not including) end, then writing them sequentially starting at target, replacing whatever was already there.

Negative indices count from the end, like other array methods. If end is omitted, copying continues through the end of the array.

💡
Beginner Tip

end is exclusive, matching slice: copyWithin(0, 3, 6) copies indexes 3, 4, and 5 (three elements) to positions starting at 0.

📝 Syntax

General form of Array.prototype.copyWithin:

JavaScript
array.copyWithin(target, start, end)

Parameters

  • target — index where pasted values begin (0-based).
  • start — first source index to copy (inclusive).
  • end — optional; stop before this index (exclusive). Defaults to array.length.

Return value

  • The modified array (same reference as the caller).
  • Length is never changed.

Common patterns

  • arr.copyWithin(0, 3, 6) — copy three elements from index 3 to the front.
  • arr.copyWithin(0, -3) — move the last three elements to the start.
  • [...arr].copyWithin(...) — mutate a clone to preserve the original.

⚡ Quick Reference

GoalCode
Copy range to targetarr.copyWithin(target, start, end)
Copy to end of arrayarr.copyWithin(target, start)
Last 3 to index 0arr.copyWithin(0, -3)
Changes length?No
Mutates array?Yes

📋 copyWithin() vs slice() vs splice()

All three touch portions of an array, but only copyWithin overwrites in place without resizing.

copyWithin
in-place overwrite

Same array, same length

slice
returns new array

Original untouched

splice
insert / remove

Length can change

end index
exclusive

Like slice range

Examples Gallery

Open DevTools Console (F12) or use Try-it labs. Example N maps to ?tryit=N.

📚 Getting Started

Copy a slice from one region to another.

Example 1 — Copy a Slice to the Front

Copy elements at 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

Source indexes 3, 4, 5 hold 4, 5, 6. Those three values overwrite indexes 0, 1, 2. The rest of the array stays as it was after the paste window.

Example 2 — Shift the Last Three Elements Forward

Use a negative start to copy from the tail without calculating 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

start = -3 resolves to index 6. With no end, copying runs through the last element. Overlap means tail values appear twice after the operation.

📈 Practical Patterns

Patterns, immutability, and default end.

Example 3 — Duplicate a Pattern Inside the Array

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

Indexes 0–2 are copied to indexes 3–5. Useful for tiling a small template across a fixed-size buffer.

Example 4 — Preserve the Original with a Shallow Clone

When you need immutability, copy the array first, then call copyWithin on the clone.

JavaScript
const original = [1, 2, 3, 4, 5];
const working = [...original];

working.copyWithin(0, 2, 4);

console.log(original);  // [1, 2, 3, 4, 5]
console.log(working);   // [3, 4, 3, 4, 5]
Try It Yourself

How It Works

copyWithin always mutates its receiver. Spread (or slice()) gives you a new top-level array so the source data stays unchanged.

Example 5 — Omit end to Copy Through the Tail

When end is omitted, copying continues to the last element.

JavaScript
const letters = ["a", "b", "c", "d", "e"];

letters.copyWithin(1, 3);

console.log(letters);
// ["a", "d", "e", "d", "e"]
Try It Yourself

How It Works

From index 3 through the end you have "d", "e". Pasting at index 1 overwrites "b", "c". Index 0 is untouched.

🚀 Common Use Cases

  • Ring buffers — rotate or slide window contents without reallocating.
  • In-place normalization — move defaults or padding within a fixed-size array.
  • Typed arrays — efficient bulk moves on binary data views.
  • Game grids — scroll tile rows/columns inside a flat representation.
  • Audio samples — shift waveform segments inside a buffer.
  • Immutable UI state — clone first, then copyWithin on the copy for Redux-style updates.

🧠 How copyWithin() Runs

1

Resolve indices

Convert target, start, and end (including negatives) to 0-based positions.

Normalize
2

Count elements

Determine how many slots to copy from the source range.

Plan
3

Overwrite target

Write each copied value into target, target+1, …

Mutate
4

Return same array

Length unchanged; method returns the array for chaining.

📝 Notes

  • end is exclusive, like slice(start, end).
  • Overlapping source and destination is allowed; engines copy in a safe order.
  • Out-of-range indices are clamped; copying zero elements is valid and is a no-op.
  • Holey (sparse) arrays copy holes as empty slots.
  • For a new array instead of mutation, use slice or spread, not copyWithin on the original.
  • Works on typed arrays such as Uint8Array with the same signature.

Browser & Runtime Support

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

Baseline · ES2015

Array.prototype.copyWithin()

Supported in Chrome 45+, Firefox 32+, Safari 9+, Edge 12+, and Node 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 web apps. For IE11, clone with slice and copy manually, or use a polyfill.

Conclusion

The copyWithin() method is the in-place tool for sliding or duplicating blocks of elements inside one array. It keeps length fixed while overwriting selected indexes.

Use it when performance and memory matter; clone first when your logic requires immutability.

💡 Best Practices

✅ Do

  • Validate indices when they come from user input
  • Clone with spread before copyWithin if immutability matters
  • Remember end is exclusive
  • Use negative start for tail-relative copies
  • Prefer copyWithin over manual loops for bulk in-array moves

❌ Don’t

  • Expect a new array—the same reference is mutated
  • Use copyWithin to grow or shrink length—use splice
  • Assume deep cloning—objects inside slots are still shared
  • Forget overlap can duplicate values visually in the result
  • Rely on it in IE11 without a fallback

Key Takeaways

Knowledge Unlocked

Five things to remember about Array.copyWithin()

Your foundation for in-place array block copies.

5
Core concepts
🔄 02

In-place

Mutates array.

Mutable
📐 03

Same length

No resize.

Fixed size
04

end exclusive

Like slice.

Range
📋 05

Clone first

When immutable.

Pattern

❓ 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 for chaining.
target is where copying begins writing. start is the first source index (inclusive). end is the stop index (exclusive)—elements copied are from start up to but not including end.
The engine copies safely even when ranges overlap, similar to memmove. Later source values may already have been overwritten when read—built-in logic handles this.
slice() returns a new shallow copy of a portion without changing the original. copyWithin() writes inside the existing array and mutates it.
copyWithin() is an ES2015 (ES6) feature. All modern browsers support it; Internet Explorer does not.
Did you know?

copyWithin behaves like a JavaScript-facing version of memmove—it is designed to copy overlapping regions correctly without you writing a manual loop backward or forward.

Continue to entries()

Learn how to iterate arrays as [index, value] pairs with an iterator.

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.

6 people found this page helpful