ArrayBuffer.prototype.resize()
Baseline2024Newly 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.
Try it
const buffer = new ArrayBuffer(8, { maxByteLength: 16 });console.log(buffer.byteLength);// Expected output: 8buffer.resize(12);console.log(buffer.byteLength);// Expected output: 12
Syntax
resize(newLength)
Parameters
newLength
The new length, in bytes, to resize the
ArrayBuffer
to.
Return value
None (undefined
).
Exceptions
TypeError
Thrown if the
ArrayBuffer
is detached or is not resizable.RangeError
Thrown if
newLength
is larger than themaxByteLength
of 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 |