ArrayBuffer.prototype.resize()
Baseline 2024Newly available
Since July 2024, this feature works across the latest devices and browser versions. This feature might not work in older devices or browsers.
Theresize() method ofArrayBuffer instances resizes theArrayBuffer to the specified size, in bytes.
In this article
Try it
const buffer = new ArrayBuffer(8, { maxByteLength: 16 });console.log(buffer.byteLength);// Expected output: 8buffer.resize(12);console.log(buffer.byteLength);// Expected output: 12Syntax
resize(newLength)Parameters
newLengthThe new length, in bytes, to resize the
ArrayBufferto.
Return value
None (undefined).
Exceptions
TypeErrorThrown if the
ArrayBufferis detached or is not resizable.RangeErrorThrown if
newLengthis larger than themaxByteLengthof theArrayBuffer.
Description
Theresize() method resizes anArrayBuffer to the size specified by thenewLength parameter, provided that theArrayBuffer isresizable and the new size is less than or equal to themaxByteLength of theArrayBuffer. New bytes are initialized to 0.
Note that you can useresize() to shrink as well as grow anArrayBuffer — it is permissible fornewLength to be smaller than theArrayBuffer's currentbyteLength.
Examples
>Using resize()
In this example, we create a 8-byte buffer that is resizable to a max length of 16 bytes, then check itsresizable property, resizing it ifresizable returnstrue:
const buffer = new ArrayBuffer(8, { maxByteLength: 16 });if (buffer.resizable) { console.log("Buffer is resizable!"); buffer.resize(12);}Specifications
| Specification |
|---|
| ECMAScript® 2026 Language Specification> # sec-arraybuffer.prototype.resize> |