UNB/ CS/ David Bremner/ teaching/ cs2613/ books/ mdn/ Reference/ Global Objects/ TypedArray/ TypedArray.prototype.copyWithin()

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.

Syntax

copyWithin(target, start)
copyWithin(target, start, end)

Parameters

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

Browser compatibility

See also