ArrayBuffer() constructor

The ArrayBuffer() constructor is used to create ArrayBuffer objects.

Try it

Syntax

new ArrayBuffer(length)

Note: ArrayBuffer() can only be constructed with new. Attempting to call it without new throws a TypeError.

Parameters

length

The size, in bytes, of the array buffer to create.

Return value

A new ArrayBuffer object of the specified size. Its contents are initialized to 0.

Exceptions

RangeError

Thrown if the length is larger than Number.MAX_SAFE_INTEGER (≥ 253) or negative.

Examples

Creating an ArrayBuffer

In this example, we create a 8-byte buffer with a Int32Array view referring to the buffer:

const buffer = new ArrayBuffer(8);
const view = new Int32Array(buffer);

Specifications

Specification
ECMAScript Language Specification
# sec-arraybuffer-constructor

Browser compatibility

BCD tables only load in the browser

Compatibility notes

Starting with ECMAScript 2015, ArrayBuffer constructors require to be constructed with a new operator. Calling an ArrayBuffer constructor as a function without new, will throw a TypeError from now on.

const buffer = ArrayBuffer(10);
// TypeError: calling a builtin ArrayBuffer constructor
// without new is forbidden
const buffer = new ArrayBuffer(10);

See also