TypedArray.prototype.set
Stores multiple values in the typed array, reading input from a specified array or typed array
Syntax
typedArray.set(source, offset?)Parameters
| Parameter | Type | Description |
|---|---|---|
| source | ArrayLike<number> | TypedArray | The source array to copy from |
| offset | number | The offset in the target at which to start writing |
Return Value
undefined
Examples
const arr = new Uint8Array(5)
arr.set([10, 20, 30])
console.log(arr) // Uint8Array [10, 20, 30, 0, 0]const arr = new Float32Array(6)
arr.set([1.0, 2.0, 3.0], 0)
arr.set([4.0, 5.0, 6.0], 3)
console.log(arr)function concat(a: Uint8Array, b: Uint8Array): Uint8Array {
const result = new Uint8Array(a.length + b.length)
result.set(a, 0)
result.set(b, a.length)
return result
}Understanding TypedArray.prototype.set
The TypedArray.prototype.set method in JavaScript stores multiple values in the typed array, reading input from a specified array or typed array. It belongs to the TypedArray object and is one of the most widely used methods for working with typedarray values in modern JavaScript and TypeScript applications.
The method signature is typedArray.set(source, offset?). It accepts 2 parameters: source, offset. When called, it returns undefined. Understanding when and how to use set() helps you write more expressive, readable code.
Common use cases for TypedArray.prototype.set include data transformation, input validation, API response processing, and building reusable utility functions. It works well alongside related methods like typedarray-subarray, typedarray-slice, uint8array-constructor, enabling you to chain operations together for complex data manipulation pipelines.
Browser support for TypedArray.prototype.set is excellent across all modern browsers including Chrome, Firefox, Safari, and Edge. It is also fully supported in Node.js and Deno. For older environments, transpilation with Babel or a polyfill may be needed.
Browser Compatibility
Supported in all modern browsers (Chrome, Firefox, Safari, Edge) and Node.js. Part of the ECMAScript standard.
Related Methods
TypedArray.prototype.subarrayReturns a new typed array on the same ArrayBuffer store and with the same element types as this typed array, from begin up to but not including end
TypedArray.prototype.sliceReturns a shallow copy of a portion of a typed array into a new typed array, with a new underlying ArrayBuffer
Uint8ArrayCreates a new Uint8Array typed array representing an array of 8-bit unsigned integers initialized to zero
More TypedArray Methods
Other methods in the TypedArray object
Related Tools
More TypedArray Methods
Explore JavaScript Methods
Browse our complete reference of 410 JavaScript methods with syntax, examples, and explanations.