TypedArray.prototype.copyWithin()
Baseline Widely available
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()
.
Try it
Syntax
copyWithin(target, start)
copyWithin(target, start, end)
Parameters
target
-
Zero-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 betweenstart
andend
are copied to succeeding indices. start
-
Zero-based index at which to start copying elements from, converted to an integer.
end
Optional-
Zero-based index at which to end copying elements from, converted to an integer.
copyWithin()
copies up to but not includingend
.
Return value
The modified typed array.
Description
See Array.prototype.copyWithin()
for more details. This method is not generic and can only be called on typed array instances.
Examples
Using copyWithin()
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 ]
Specifications
Specification |
---|
ECMAScript Language Specification # sec-%typedarray%.prototype.copywithin |
Browser compatibility
BCD tables only load in the browser