ArrayBuffer() constructor
Baseline
Widely available
*
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
* Some parts of this feature may have varying levels of support.
The ArrayBuffer() constructor creates ArrayBuffer objects.
Try it
// Create an ArrayBuffer with a size in bytes
const buffer = new ArrayBuffer(8);
console.log(buffer.byteLength);
// Expected output: 8
Syntax
new ArrayBuffer(length)
new ArrayBuffer(length, options)
Parameters
length-
The size, in bytes, of the array buffer to create.
optionsOptional-
An object, which can contain the following properties:
maxByteLengthOptional-
The maximum size, in bytes, that the array buffer can be resized to.
Return value
A new ArrayBuffer object of the specified size, with its maxByteLength property set to the specified maxByteLength if one was specified. Its contents are initialized to 0.
Exceptions
RangeError-
Thrown in one of the following cases:
lengthormaxByteLengthis larger thanNumber.MAX_SAFE_INTEGER(≥ 253) or negative.lengthis larger thanmaxByteLength.
Examples
>Creating an ArrayBuffer
In this example, we create a 8-byte buffer with an Int32Array view referring to the buffer:
const buffer = new ArrayBuffer(8);
const view = new Int32Array(buffer);
Creating a resizable ArrayBuffer
In this example, we create a 8-byte buffer that is resizable to a max length of 16 bytes, then resize() it to 12 bytes:
const buffer = new ArrayBuffer(8, { maxByteLength: 16 });
buffer.resize(12);
Note:
It is recommended that maxByteLength is set to the smallest value possible for your use case. It should never exceed 1073741824 (1GB) to reduce the risk of out-of-memory errors.
Specifications
| Specification |
|---|
| ECMAScript® 2026 Language Specification> # sec-arraybuffer-constructor> |