The ArrayBuffer()
constructor is used to create ArrayBuffer
objects.
ArrayBuffer() constructor
Try it
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.
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
Browser compatibility
Desktop | Mobile | Server | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | WebView Android | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | Deno | Node.js | |
ArrayBuffer |
7
|
12
|
4
|
10
|
11.6
|
5.1
|
4
|
18
|
4
|
12
|
4.2
|
1.0
|
1.0
|
0.10.0
|
new_required |
7
|
14
|
44
|
No
|
15
|
5.1
|
≤37
|
18
|
44
|
14
|
5
|
1.0
|
1.0
|
0.12.0
|
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
© 2005–2022 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer/ArrayBuffer