typedArray.copyWithin(target, start, end) copies the elements from start up to, but not including, end to the position targetinside the same typed array. The length never changes, and anything that would run past the end is dropped. The copy is overlap-safe, and the method returns the same typed array, so it is handy for shifting data inside a fixed buffer.
Remember
ta.copyWithin(target, start)
ta.copyWithin(target, start, end)
[1,2,3,4,5,6].copyWithin(0, 3) → 4 5 6 4 5 6
[1,2,3,4,5].copyWithin(1, 0) → 1 1 2 3 4
length never changes, returns the same array
Syntax
The target is required. Start and end are optional and the end index is not included.
Copy everything from index 3 to the end to the beginning.
ta.copyWithin(2, 0, 2)
Copy the elements at indexes 0 and 1 to indexes 2 and 3.
ta.copyWithin(-2, 0)
Negative numbers count from the end: write at the second-to-last slot.
Available since ES2015 on all 11 types. The same method exists on regular Arrays, but a typed array can never grow or shrink.
Library declarations
JavaScript
TypedArray.prototype.copyWithin(target, start, end = length)
// negative numbers count from the end
// copies at most length - target elements
// overlapping ranges are copied safely
// returns the SAME typed array (this)
Parameters
Parameter
Description
target
Index where the copied elements are written. Negative counts from the end.
start (optional)
Index of the first element to copy. Negative counts from the end. Defaults to 0.
end (optional)
Index to stop before. Negative counts from the end. Defaults to the length.
Return Value
Return Value
Description
TypedArray
The same typed array you called it on, changed in place. Its length is the same as before.
JavaScript Examples
Example 1 — Copy Part of the Array Forward
Six samples are stored in a Uint8Array. We copy the second half to the front, then a smaller range into the middle.
-2 is the second-to-last index. On five elements, copyWithin(-2, 0, 2) writes the first two values at indexes 3 and 4.
A negative start works too.copyWithin(0, -2) reads the last two elements, 4 and 5, and puts them at the front.
A target past the end does nothing. Index 5 is outside a three-element array, so there is no room to write, and no error is thrown.
Compatibility
Browser & Runtime Support
TypedArray.prototype.copyWithin() is part of ES2015 (ES6), together with the other array-like typed array methods.
✓ Baseline · ES2015
TypedArray.prototype.copyWithin()
Supported in every modern browser, plus Node.js, Deno, and Bun.
98%Modern browser support
Google ChromeAll modern versions
Full support
Mozilla FirefoxAll modern versions
Full support
Apple SafariAll modern versions
Full support
Microsoft EdgeAll modern versions
Full support
Node.jsAll current releases
Supported
DenoAll releases
Supported
Internet ExplorerIE 10 and 11 have typed arrays, but not this method
Not supported
copyWithin()Excellent
Bottom line: Safe everywhere modern JavaScript runs. Internet Explorer 10 and 11 have typed arrays, but not the copyWithin() method.
Frequently Asked Questions
It copies a range of elements to another position in the same typed array. It changes the array in place, keeps its length, and returns the same typed array.
Yes. The elements are copied as if they were saved first, so copyWithin(1, 0) on 1 2 3 4 5 gives 1 1 2 3 4.
No. The source elements stay where they are unless they get overwritten. Use fill() afterwards if you want to reset them.
Only the elements that fit are copied, and the rest are dropped. A target past the end copies nothing. The length never changes and no error is thrown.
copyWithin() moves values inside one typed array. set() copies from another array or typed array into the target, and throws a RangeError if the source does not fit.