The ArrayBuffer()
constructor is used to create ArrayBuffer
objects.
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
Syntax
new ArrayBuffer(length)
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
A RangeError
is thrown if the length
is larger than Number.MAX_SAFE_INTEGER
(>= 2 ** 53) or negative.
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.
var dv = ArrayBuffer(10); // TypeError: calling a builtin ArrayBuffer constructor // without new is forbidden
var dv = new ArrayBuffer(10);
Examples
Creating an ArrayBuffer
In this example, we create a 8-byte buffer with a Int32Array
view referring to the buffer:
var buffer = new ArrayBuffer(8); var view = new Int32Array(buffer);
Specifications
Specification |
---|
ECMAScript (ECMA-262) The definition of 'ArrayBuffer' in that specification. |
Browser compatibility
Desktop | Mobile | Server | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
ArrayBuffer() constructor | Chrome Full support 7 | Edge Full support 12 | Firefox Full support 4 | IE Full support 10 | Opera Full support 11.6 | Safari Full support 5.1 | WebView Android Full support 4 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support 12 | Safari iOS Full support 4.2 | Samsung Internet Android Full support 1.0 | nodejs Full support 0.10 |
ArrayBuffer() without new throws | Chrome Full support 7 | Edge Full support 14 | Firefox Full support 44 | IE No support No | Opera Full support 15 | Safari Full support 5.1 | WebView Android Full support ≤37 | Chrome Android Full support 18 | Firefox Android Full support 44 | Opera Android Full support 14 | Safari iOS Full support 5 | Samsung Internet Android Full support 1.0 | nodejs Full support 0.12 |
Legend
- Full support
- Full support
- No support
- No support