You can create a new TypedArray with a new ArrayBuffer, but you can't change the size of an existing buffer

function concatTypedArrays(a, b) { // a, b TypedArray of same type
    var c = new (a.constructor)(a.length + b.length);
    c.set(a, 0);
    c.set(b, a.length);
    return c;
}

Now can do

var a = new Uint8Array(2),
    b = new Uint8Array(3);
a[0] = 1; a[1] = 2;
b[0] = 3; b[1] = 4;
concatTypedArrays(a, b); // [1, 2, 3, 4, 0] Uint8Array length 5

If you want to use different types, go via Uint8Array as the smallest unit is a byte, i.e.

function concatBuffers(a, b) {
    return concatTypedArrays(
        new Uint8Array(a.buffer || a), 
        new Uint8Array(b.buffer || b)
    ).buffer;
}

This means .length will work as expected, you could now convert this to your typed array of choice (make sure it's a type that would accept the .byteLength of the buffer though)


From here, you could now implement any method you like for concatenating your data, e.g.

function concatBytes(ui8a, byte) {
    var b = new Uint8Array(1);
    b[0] = byte;
    return concatTypedArrays(ui8a, b);
}

var u8 = new Uint8Array(0);
u8 = concatBytes(u8, 0x80); // [128]
Answer from Paul S. on Stack Overflow
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Uint8Array
Uint8Array - JavaScript - MDN Web Docs
The Uint8Array typed array represents an array of 8-bit unsigned integers. The contents are initialized to 0 unless initialization data is explicitly provided. Once established, you can reference elements in the array using the object's methods, or using standard array index syntax (that is, using bracket notation).
🌐
Evan Hahn
evanhahn.com › the-best-way-to-concatenate-uint8arrays
What's the best way to concatenate Uint8Arrays? - Evan Hahn
January 29, 2024 - // Warning: this solution is bad! function badConcatenate(uint8arrays) { const array = []; for (const uint8array of uint8arrays) { array.push(...uint8array); } return new Uint8Array(array); }
Top answer
1 of 2
40

You can create a new TypedArray with a new ArrayBuffer, but you can't change the size of an existing buffer

function concatTypedArrays(a, b) { // a, b TypedArray of same type
    var c = new (a.constructor)(a.length + b.length);
    c.set(a, 0);
    c.set(b, a.length);
    return c;
}

Now can do

var a = new Uint8Array(2),
    b = new Uint8Array(3);
a[0] = 1; a[1] = 2;
b[0] = 3; b[1] = 4;
concatTypedArrays(a, b); // [1, 2, 3, 4, 0] Uint8Array length 5

If you want to use different types, go via Uint8Array as the smallest unit is a byte, i.e.

function concatBuffers(a, b) {
    return concatTypedArrays(
        new Uint8Array(a.buffer || a), 
        new Uint8Array(b.buffer || b)
    ).buffer;
}

This means .length will work as expected, you could now convert this to your typed array of choice (make sure it's a type that would accept the .byteLength of the buffer though)


From here, you could now implement any method you like for concatenating your data, e.g.

function concatBytes(ui8a, byte) {
    var b = new Uint8Array(1);
    b[0] = byte;
    return concatTypedArrays(ui8a, b);
}

var u8 = new Uint8Array(0);
u8 = concatBytes(u8, 0x80); // [128]
2 of 2
10

Paul's answer allows you to concatenate one TypedArray to an existing TypedArray. In ES6, you can use the following function to concatenate multiple TypedArrays:

function concatenate(resultConstructor, ...arrays) {
    let totalLength = 0;
    for (const arr of arrays) {
        totalLength += arr.length;
    }
    const result = new resultConstructor(totalLength);
    let offset = 0;
    for (const arr of arrays) {
        result.set(arr, offset);
        offset += arr.length;
    }
    return result;
}

const ta = concatenate(Uint8Array,
    Uint8Array.of(1, 2), Uint8Array.of(3, 4));
console.log(ta); // Uint8Array [1, 2, 3, 4]
console.log(ta.buffer.byteLength); // 4

To append a new byte is:

const byte = 3;
concatenate(Uint8Array, Uint8Array.of(1, 2), Uint8Array.of(byte));

This method is found in ExploringJS.

🌐
JavaScripture
javascripture.com › Uint8Array
Uint8Array JavaScript API
Creates a new Uint8Array and copies the items of array into this.
🌐
JavaScript.info
javascript.info › tutorial › binary data, files
ArrayBuffer, binary arrays
July 11, 2022 - Uint8Array – treats each byte in ArrayBuffer as a separate number, with possible values from 0 to 255 (a byte is 8-bit, so it can hold only that much).
🌐
Medium
medium.com › @lagerdata › a-resizable-typedarray-f5b9a861958
A resizable TypedArray. One of the most powerful features of… | by Lager | Medium
August 29, 2019 - Our solution was to create a class GrowableUint8Array that wraps an instance of Uint8Array, automatically reallocating a larger buffer when necessary, but using an exponentially increasing size for the buffer, drastically reducing the number of allocations and copies necessary.
🌐
Yjs
docs.yjs.dev › api › shared-types › y.array
Y.Array | Yjs Docs
July 30, 2024 - I.e. yarray.insert(0, [1]) inserts ... index. yarray.push(content: Array<JSON | Uint8Array | Y.AbstractType>) Append content at the end of the Y.Array....
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Uint8Array › Uint8Array
Uint8Array() constructor - JavaScript - MDN Web Docs
July 10, 2025 - The Uint8Array() constructor creates Uint8Array objects. The contents are initialized to 0 unless initialization data is explicitly provided.
Find elsewhere
🌐
GitHub
github.com › microsoft › TypeScript › issues › 31311
TS allows adding Uint8Array item to Array<ArrayBuffer> · Issue #31311 · microsoft/TypeScript
May 8, 2019 - Search Terms: ArrayBuffer, Uint8Array · Playground link · interface SimpleReadStream {onData: (data: ArrayBuffer) => any;} declare function createFileReadStream(name: string): SimpleReadStream; // CASE 1 const stream1: SimpleReadStream = createFileReadStream(name); const blobData1: ArrayBuffer[] = []; stream1.onData = data => { // TYPE MISMATCH (but no error) blobData1.push(new Uint8Array(data)); }; // CASE 2 const stream2: SimpleReadStream = createFileReadStream(name); const blobData2: Uint8Array[] = []; stream2.onData = data => { // TYPE MISMATCH (error as expected) blobData2.push(data); }; Expected behavior: Both test cases should result in type errors.
Author: microsoft
🌐
Bun
bun.sh › guides › binary › typedarray-to-buffer
Convert a Uint8Array to a Buffer - Bun
Use Buffer.from() to create a Buffer instance from a Uint8Array.
🌐
GitHub
gist.github.com › 72lions › 4528834
Concatenates two ArrayBuffers · GitHub
function concatArrayBuffers (bufs) { var offset = 0; var bytes = 0; var bufs2=bufs.map(function(buf,total){ bytes += buf.byteLength; return buf; }); var buffer = new ArrayBuffer(bytes); var store = new Uint8Array(buffer); bufs2.forEach(function(buf){ store.set(new Uint8Array(buf.buffer||buf,buf.byteOffset),offset); offset += buf.byteLength; }); return buffer } var all_buffers = concatArrayBuffers([buffer1,buffer2,buffer3]);
🌐
Tabnine
tabnine.com › home › code library
Code Library - Tabnine
July 25, 2024 - Get the answers and suggestions you need from our AI code assistant. Get started in minutes with a free 90 day trial of Tabnine Pro.
🌐
GitHub
github.com › whatwg › fetch › issues › 1732
add a method to get a Uint8Array to Body · Issue #1732 · whatwg/fetch
January 29, 2024 - Add a .bytes() method which gives a new Uint8Array. Some brief discussion here. PushMessageData and Blob (which live elsewhere) would need this too, since they just copied Body.
Author: whatwg
🌐
W3Schools
w3schools.com › jsref › jsref_typed_uint8array.asp
JavaScript Uint8Array
Uint8Array array represents a typed array of 8-bit unsigned integers.
Starred by 9 users
Forked by 9 users
Languages: TypeScript 92.1% | JavaScript 7.9% | TypeScript 92.1% | JavaScript 7.9%
🌐
GitHub
github.com › web-push-libs › web-push › issues › 558
applicationServerKey to Uint8Array · Issue #558 · web-push-libs/web-push
March 2, 2020 - According to MDN https://developer.mozilla.org/en-US/docs/Web/API/PushManager/subscribe applicationServerKey can be either DOMString or ArrayBuffer. web-push documentation on the other hand asks to convert VAPID public key to Uint8Array using urlBase64ToUint8Array example function.
Author: web-push-libs
🌐
Duktape
wiki.duktape.org › howtobuffers2x
How to work with buffers in Duktape 2.x
Plain buffers only provide an uint8 access into an underlying buffer. Plain buffers can be fixed, dynamic (resizable), or external (point to user controlled buffer outside of Duktape control). A plain buffer value object coerces to a Uint8Array object, similarly to how a plain string object coerces to String object.