The resize()
method of ArrayBuffer instances resizes the ArrayBuffer
to the specified size, in bytes.
Syntax
resize(newLength)
Parameters
newLength
- : The new length, in bytes, to resize the
ArrayBuffer
to.
- : The new length, in bytes, to resize the
Return value
None (undefined).
Exceptions
- TypeError
- : Thrown if the
ArrayBuffer
is detached or is not resizable.
- : Thrown if the
- RangeError
- : Thrown if
newLength
is larger than the maxByteLength of theArrayBuffer
.
- : Thrown if
Description
The resize()
method resizes an ArrayBuffer
to the size specified by the newLength
parameter, provided that the ArrayBuffer
is resizable and the new size is less than or equal to the maxByteLength of the ArrayBuffer
. New bytes are initialized to 0.
Note that you can use resize()
to shrink as well as grow an ArrayBuffer
— it is permissible for newLength
to be smaller than the ArrayBuffer
's current byteLength.
Examples
Using resize()
In this example, we create a 8-byte buffer that is resizable to a max length of 16 bytes, then check its resizable
property, resizing it if resizable
returns true
:
const buffer = new ArrayBuffer(8, { maxByteLength: 16 });
if (buffer.resizable) {
console.log("Buffer is resizable!");
buffer.resize(12);
}