typedArray.set(source, offset)copies the values of an array or another typed array into an existing typed array, starting at index offset (default 0). It changes the typed array in place and returns undefined. Values are converted to the element type of the target. If the source does not fit between offset and the end, a RangeError is thrown and nothing is written.
Remember
ta.set(source) → copy in at index 0
ta.set(source, offset) → copy in at index offset
new Uint8Array(6).set([1, 2, 3], 2) → 0 0 1 2 3 0
argument: array or typed array
too big: RangeError (nothing is copied)
returns: undefined
Syntax
Give the source and, optionally, the index where writing should start.
Copy inside the same buffer. The overlap is handled safely.
Part of the original typed array API and in ES2015 on all 11 types. A BigInt array needs a BigInt source, and the source must not mix BigInt and Number arrays.
Library declarations
JavaScript
TypedArray.prototype.set(source, offset = 0)
// first argument: array, array-like, or typed array
// offset + source.length must be <= ta.length, otherwise RangeError
// negative offset -> RangeError
// returns undefined
Parameters
Parameter
Description
source
An array, array-like object, or typed array. The values are converted to the element type of the target.
offset (optional)
The index in the target where the first value is written. Defaults to 0. A negative offset throws a RangeError.
Return Value
Return Value
Description
undefined
Nothing. set() works in place, so use the typed array itself afterwards. It is not chainable.
JavaScript Examples
Example 1 — Write Values at an Offset
A small packet buffer starts empty. We write a header, then a payload at a later position, then bytes from an Int16Array.