TypedArray

TypedArray.prototype.set

Stores multiple values in the typed array, reading input from a specified array or typed array

Syntax

JavaScript
typedArray.set(source, offset?)

Parameters

ParameterTypeDescription
sourceArrayLike<number> | TypedArrayThe source array to copy from
offsetnumberThe offset in the target at which to start writing

Return Value

undefined

Examples

Basic Usage
const arr = new Uint8Array(5)
arr.set([10, 20, 30])
console.log(arr) // Uint8Array [10, 20, 30, 0, 0]
Practical Example
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)
Advanced Usage
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

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.