JavaScript ArrayBuffer transferToFixedLength() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
ArrayBuffer

Definition

buffer.transferToFixedLength(newLength) works like transfer(): it moves the bytes into a new ArrayBuffer and detaches the original. The difference is the result, which is always fixed-length (resizable is false), even when the source was resizable. Use it to lock a buffer at its final size once you have finished building it. Added in ES2024.

Remember
const growable = new ArrayBuffer(4, { maxByteLength: 64 });
const done = growable.transferToFixedLength();

done.resizable       → false (always)
done.maxByteLength   → same as done.byteLength
growable.detached    → true

transferToFixedLength(10) → 10 bytes, extra bytes are 0
the old maxByteLength does not limit the new size

Syntax

Call transferToFixedLength on the buffer to retire, optionally with the final length.

JavaScript
buffer.transferToFixedLength()
buffer.transferToFixedLength(newLength)
CallWhat it does
buffer.transferToFixedLength()Move the bytes into a fixed-length buffer of the same size.
buffer.transferToFixedLength(newLength)Move the bytes into a fixed-length buffer of newLength bytes.

Built into JavaScript since ES2024. Because the result is fixed-length, newLength is not limited by the old maxByteLength.

Library declarations
JavaScript
buffer.transferToFixedLength(newLength)   // returns a new ArrayBuffer

// newLength: default buffer.byteLength
// result.resizable === false
// result.maxByteLength === result.byteLength
// afterwards: buffer.detached === true

Parameters

ParameterDescription
newLengthOptional. Size in bytes of the new buffer (default: the current byteLength). Larger values add zero bytes at the end; smaller values drop bytes from the end.

Return Value

Return ValueDescription
ArrayBufferA new, fixed-length ArrayBuffer that owns the data. The original is detached.

JavaScript Examples

Example 1 — Lock a Finished Buffer at Its Exact Size

Build data in a growable buffer, then lock it at its exact size before storing it or passing it on.

JavaScript
const draft = new ArrayBuffer(4, { maxByteLength: 64 });
const bytes = new Uint8Array(draft);
bytes.set([7, 8, 9]);

const finished = draft.transferToFixedLength(3);

console.log(finished.byteLength);  // 3
console.log(finished.resizable);   // false
console.log(JSON.stringify(Array.from(new Uint8Array(finished))));  // [7,8,9]
console.log(draft.detached);       // true
Try It Yourself

How It Works

  1. Draft in a growable buffer. draft has 4 bytes of room (and could grow to 64), but only 3 bytes hold data.
  2. Trim and lock in one step. transferToFixedLength(3) drops the unused fourth byte and returns a buffer whose size can never change.
  3. The draft is retired. The bytes now live in finished; draft is detached, so nothing can keep writing to the old buffer.

Example 2 — transfer() vs transferToFixedLength()

Start from two identical resizable buffers and move each one with a different method.

JavaScript
const a = new ArrayBuffer(4, { maxByteLength: 16 });
const b = new ArrayBuffer(4, { maxByteLength: 16 });

const keptResizable = a.transfer();
const madeFixed = b.transferToFixedLength();

console.log(keptResizable.resizable);      // true
console.log(keptResizable.maxByteLength);  // 16
console.log(madeFixed.resizable);          // false
console.log(madeFixed.maxByteLength);      // 4

try {
  madeFixed.resize(8);
} catch (err) {
  console.log(err.name);                   // TypeError
}
Try It Yourself

How It Works

  1. Two identical sources. Both a and b are 4-byte resizable buffers with a 16-byte limit.
  2. transfer() keeps the option. keptResizable is still resizable and still has maxByteLength 16.
  3. transferToFixedLength() drops it. For a fixed buffer maxByteLength simply equals byteLength (4), and resize() throws a TypeError.

Example 3 — Pad, Cut, and Reject Bad Lengths

The new length can be larger or smaller than the old one. A negative length is rejected before anything moves.

JavaScript
const show = (buf) => JSON.stringify(Array.from(new Uint8Array(buf)));
const source = new Uint8Array([5, 6]).buffer;

try {
  source.transferToFixedLength(-1);
} catch (err) {
  console.log(err.name);         // RangeError
}
console.log(source.detached);    // false

const padded = source.transferToFixedLength(5);
console.log(show(padded));       // [5,6,0,0,0]

const cut = padded.transferToFixedLength(1);
console.log(show(cut));          // [5]
console.log(padded.byteLength);  // 0
Try It Yourself

How It Works

  1. Bad length, nothing moves. A negative length throws a RangeError before the transfer starts, so source is still attached and usable.
  2. Pad with zeros. transferToFixedLength(5) keeps the two bytes and fills the remaining three with 0.
  3. Cut down. Moving padded into a 1-byte buffer keeps only its first byte, and padded then reports byteLength 0.

Browser & Runtime Support

transferToFixedLength() arrived with transfer() and the detached property in ES2024.

✓ Newer · ES2024

ArrayBuffer.prototype.transferToFixedLength()

In recent Chrome, Edge, Firefox, Safari, and Opera, and in current Node.js (22 LTS and newer; the examples were run on Node.js 24). Check a compatibility table such as MDN's for your browser targets.

Recent Recent browsers only
Google Chrome Recent versions
Supported
Mozilla Firefox Recent versions
Supported
Apple Safari Recent versions
Supported
Microsoft Edge Recent versions
Supported
Node.js Node.js 22 LTS+ · check with typeof
Supported
Deno Recent versions
Supported
Internet Explorer No support
Not supported
transferToFixedLength() Recent browsers

Bottom line: Feature-detect with typeof ArrayBuffer.prototype.transferToFixedLength === "function". A rough fallback is buffer.slice(0, length): also fixed-length, but it copies and leaves the original attached.

Frequently Asked Questions

It moves the bytes of an ArrayBuffer into a new fixed-length ArrayBuffer and detaches the original. You can pass a new length to pad with zeros or cut bytes off the end.
Use it when the data is complete and its size should never change again, for example after building a message in a resizable buffer. Use transfer() when the new owner may still need to resize.
Yes. The result is a fixed-length buffer, so the old maxByteLength limit does not apply. transferToFixedLength(100) on a buffer with maxByteLength 64 returns a 100-byte buffer.
Yes. For a fixed-length source it behaves like transfer(): the data moves to a new fixed-length buffer and the original is detached.
It arrived with transfer() in ES2024. Recent Chrome, Edge, Firefox, and Safari support it, as does current Node.js. Feature-detect with typeof ArrayBuffer.prototype.transferToFixedLength === "function".

Back to the ArrayBuffer Method Index

Review every ArrayBuffer method and property in one place.

ArrayBuffer methods →

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