JavaScript TypedArray set() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
ES2015

Definition

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.

JavaScript
typedArray.set(source)
typedArray.set(source, offset)
CallWhat it does
ta.set([1, 2, 3])Write three numbers at the start.
ta.set(other, 4)Write another typed array starting at index 4.
ta.set(ta.subarray(0, 4), 2)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

ParameterDescription
sourceAn 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 ValueDescription
undefinedNothing. 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.

JavaScript
const packet = new Uint8Array(6);

packet.set([1, 2, 3]);
console.log(packet.join(' '));   // 1 2 3 0 0 0

packet.set([9, 8], 4);
console.log(packet.join(' '));   // 1 2 3 0 9 8

packet.set(new Int16Array([300, -1]), 3);
console.log(packet.join(' '));   // 1 2 3 44 255 8

console.log(packet.set([5]) === undefined);   // true
Try It Yourself

How It Works

  1. The default offset is 0. The first call overwrites indexes 0 to 2 and leaves the three zeros alone.
  2. The offset moves the start. offset 4 writes the two values at indexes 4 and 5 and replaces the older 0 and 0.
  3. Typed array sources are converted. The Int16 values 300 and -1 become 44 and 255 in the byte array. The call itself returns undefined.

Example 2 — When the Source Does Not Fit

The target has four elements. Writing three values at index 2 would need five, so set() refuses and leaves the data untouched.

JavaScript
const slots = new Uint8Array(4);

try {
  slots.set([1, 2, 3], 2);
} catch (e) {
  console.log(e.name);        // RangeError
}
console.log(slots.join(' '));  // 0 0 0 0

try {
  slots.set([1], -1);
} catch (e) {
  console.log(e.name);        // RangeError
}

slots.set([1, 2, 3], 1);
console.log(slots.join(' '));  // 0 1 2 3
Try It Yourself

How It Works

  1. Too long by one. Index 2 plus three values reaches index 4, but the last valid index is 3, so a RangeError is thrown.
  2. Nothing is half-written. The check happens first, so all four slots still print as 0.
  3. Negative offsets and the fix. An offset of -1 also throws. Starting at index 1 fits exactly, and the array becomes 0 1 2 3.

Example 3 — Copy Inside the Same Buffer

A view of the same bytes can be the source. set() copies as if the source were taken first, so the overlap does no damage.

JavaScript
const data = new Uint8Array([1, 2, 3, 4, 5, 6]);

data.set(data.subarray(0, 4), 2);
console.log(data.join(' '));          // 1 2 1 2 3 4

const left = data.subarray(0, 3);
const right = data.subarray(3);
right.set([7, 7]);
console.log(left.join(' ') + ' | ' + right.join(' '));   // 1 2 1 | 7 7 4
console.log(data.join(' '));                              // 1 2 1 7 7 4
Try It Yourself

How It Works

  1. Source and target share bytes. data.subarray(0, 4) is a view of the first four bytes, and they are copied to start at index 2.
  2. The result is a clean shift. Indexes 2 to 5 now hold the old 1, 2, 3, 4. A careless one-by-one loop would have smeared 1 and 2 over everything.
  3. Views see the change. Writing through right changes indexes 3 and 4 of data, and left and data show the same bytes.

Browser & Runtime Support

TypedArray.prototype.set() was in the first typed array API and is part of ES2015 (ES6).

✓ Baseline · ES2015

TypedArray.prototype.set()

Supported in every modern browser, plus Node.js, Deno, and Bun.

98% Modern browser support
Google Chrome All modern versions
Full support
Mozilla Firefox All modern versions
Full support
Apple Safari All modern versions
Full support
Microsoft Edge All modern versions
Full support
Node.js All current releases
Supported
Deno All releases
Supported
Internet Explorer IE 10 and newer
Full support
set() Excellent

Bottom line: Safe everywhere. Even Internet Explorer 10 and 11 had set(), because it came with the very first typed arrays.

Frequently Asked Questions

It copies values from an array or typed array into the target, starting at an offset. It changes the target in place and returns undefined.
When offset plus the source length is bigger than the target length, or when the offset is negative. Nothing is written in that case.
No. Typed arrays have a fixed length. Create a larger typed array first, then use set() to copy the old and new values into it.
Yes. The copy behaves as if the source values were read first, so overlapping ranges, like data.set(data.subarray(0, 4), 2), give the correct result.
set() copies from any source into the target. copyWithin() copies one range of the same typed array to another place inside itself.

Next: slice()

Copy a range of elements into a brand-new, independent typed array.

slice() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful