The ArrayBuffer
object is used to represent a generic, fixed-length raw binary data buffer. You can not directly manipulate the contents of an ArrayBuffer
; instead, you create one of the typed array objects or a {{jsxref("DataView")}} object which represents the buffer in a specific format, and use that to read and write the contents of the buffer.
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.
Description
The ArrayBuffer
constructor creates a new ArrayBuffer
of the given length in bytes.
Getting an array buffer from existing data
Properties
ArrayBuffer.length
- The
ArrayBuffer
constructor's length property whose value is 1. - {{jsxref("ArrayBuffer.prototype")}}
- Allows the addition of properties to all
ArrayBuffer
objects.
Methods
- {{jsxref("ArrayBuffer.isView", "ArrayBuffer.isView(arg)")}}
- Returns
true
ifarg
is one of the ArrayBuffer views, such as typed array objects or a {{jsxref("DataView")}}. Returnsfalse
otherwise. - {{jsxref("ArrayBuffer.transfer", "ArrayBuffer.transfer(oldBuffer [, newByteLength])")}} {{experimental_inline}}
-
Returns a new
ArrayBuffer
whose contents are taken from theoldBuffer
's data and then is either truncated or zero-extended bynewByteLength
.
ArrayBuffer
instances
All ArrayBuffer
instances inherit from {{jsxref("ArrayBuffer.prototype")}}.
Properties
{{page('en-US/Web/JavaScript/Reference/Global_Objects/ArrayBuffer/prototype','Properties')}}
Methods
{{page('en-US/Web/JavaScript/Reference/Global_Objects/ArrayBuffer/prototype','Methods')}}
Example
In this example, we create a 8-byte buffer with a {{jsxref("Global_Objects/Int32Array", "Int32Array")}} view referring to the buffer:
var buffer = new ArrayBuffer(8); var view = new Int32Array(buffer);
Specifications
Specification | Status | Comment |
---|---|---|
{{SpecName('Typed Array')}} | {{Spec2('Typed Array')}} | Superseded by ECMAScript 6. |
{{SpecName('ES6', '#sec-arraybuffer-constructor', 'ArrayBuffer')}} | {{Spec2('ES6')}} | Initial definition in an ECMA standard. Specified that new is required. |
{{SpecName('ESDraft', '#sec-arraybuffer-constructor', 'ArrayBuffer')}} | {{Spec2('ESDraft')}} |
Browser compatibility
{{CompatibilityTable}}
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|
Basic support | 7.0 | {{CompatGeckoDesktop("2")}} | 10 | 11.6 | 5.1 |
ArrayBuffer() without new throws |
{{CompatUnknown}} | {{CompatGeckoDesktop("44")}} | {{CompatUnknown}} | {{CompatUnknown}} | {{CompatUnknown}} |
Feature | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|
Basic support | 4.0 | {{CompatVersionUnknown}} | {{CompatGeckoMobile("2")}} | 10 | 11.6 | 4.2 |
ArrayBuffer() without new throws |
{{CompatUnknown}} | {{CompatUnknown}} | {{CompatGeckoMobile("44")}} | {{CompatUnknown}} | {{CompatUnknown}} | {{CompatUnknown}} |
Compatibility notes
Starting with ECMAScript 2015, ArrayBuffer
constructors require to be constructed with a {{jsxref("Operators/new", "new")}} operator. Calling an ArrayBuffer
constructor as a function without new
, will throw a {{jsxref("TypeError")}} from now on.
var dv = ArrayBuffer(10); // TypeError: calling a builtin ArrayBuffer constructor // without new is forbidden
var dv = new ArrayBuffer(10);
See also
- JavaScript typed arrays
- {{jsxref("SharedArrayBuffer")}}