This feature is well established and works across many devices and browser versions. It’s been available across browsers since September 2016.
The copyWithin() method of TypedArray instances shallow copies part of this typed array to another location in the same typed array and returns this typed array without modifying its length. This method has the same algorithm as Array.prototype.copyWithin().
const uint8 = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]); // Insert position, start position, end position uint8.copyWithin(3, 1, 3); console.log(uint8); // Expected output: Uint8Array [1, 2, 3, 2, 3, 6, 7, 8]
copyWithin(target, start) copyWithin(target, start, end)
targetZero-based index at which to copy the sequence to, converted to an integer. This corresponds to where the element at start will be copied to, and all elements between start and end are copied to succeeding indices.
startZero-based index at which to start copying elements from, converted to an integer.
end OptionalZero-based index at which to end copying elements from, converted to an integer. copyWithin() copies up to but not including end.
The modified typed array.
See Array.prototype.copyWithin() for more details. This method is not generic and can only be called on typed array instances.
const buffer = new ArrayBuffer(8); const uint8 = new Uint8Array(buffer); uint8.set([1, 2, 3]); console.log(uint8); // Uint8Array [ 1, 2, 3, 0, 0, 0, 0, 0 ] uint8.copyWithin(3, 0, 3); console.log(uint8); // Uint8Array [ 1, 2, 3, 1, 2, 3, 0, 0 ]
| Desktop | Mobile | Server | |||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Opera | Safari | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | WebView Android | WebView on iOS | Bun | Deno | Node.js | |
copyWithin |
45 | 12 | 34 | 32 | 10 | 45 | 34 | 32 | 10 | 5.0 | 45 | 10 | 1.0.0 | 1.0 | 4.0.0 |
© 2005–2025 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/copyWithin