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
Fundamentals
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.
Concept
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.
Foundation
📝 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.
Cheat Sheet
⚡ Quick Reference
Goal
Code
Copy range to target
arr.copyWithin(target, start, end)
Copy to end of array
arr.copyWithin(target, start)
Last 3 to index 0
arr.copyWithin(0, -3)
Changes length?
No
Mutates array?
Yes
Compare
📋 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
Hands-On
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.
From index 3 through the end you have "d", "e". Pasting at index 1 overwrites "b", "c". Index 0 is untouched.
Applications
🚀 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.
Important
📝 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.
Compatibility
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 ChromeSupported · Desktop & Mobile
Full support
Mozilla FirefoxSupported · Desktop & Mobile
Full support
Apple SafariSupported · macOS & iOS
Full support
Microsoft EdgeSupported · Chromium
Full support
Internet ExplorerNo native support · Use a polyfill
Polyfill
OperaSupported · Modern versions
Full support
Samsung InternetSupported · Android
Full support
BunSupported · JavaScript runtime
Supported
DenoSupported · JavaScript runtime
Supported
Node.jsSupported · Server runtime
Supported
Android WebViewSupported · 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.
Wrap Up
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.
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
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about Array.copyWithin()
Your foundation for in-place array block copies.
5
Core concepts
📝01
Syntax
(target, start, end?)
API
🔄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.