JavaScript | ArrayBuffer Object
An ArrayBuffer object is used to represent a generic, fixed-length raw binary data buffer. The contents of an ArrayBuffer cannot be directly manipulated and can only be accessed through a DataView Object or one of the typed array objects. These Objects are used to read and write the contents of the buffer. More than one DataView or typed array objects can be added to one ArrayBuffer and any changes to one object can be easily seen by the other objects view.
The following are the typed arrays:
Float32Array, Float64Array, Int8Array, Int16Array, Int32Array, Uint8Array, Uint8ClampedArray, Uint16Array, Uint32Array.
Syntax:
new ArrayBuffer(byteLength)
Parameters: It accepts one parameter i.e. bytelength which denotes the size, in bytes, of the array buffer to be created.
Return value: It returns a new ArrayBuffer object of the specified size and the content is initialized to 0.
<script> //Create a 16byte buffer var buffer = new ArrayBuffer(16); //Create a DataView referring to the buffer var view1 = new DataView(buffer); //Create a Int8Array view referring to the buffer var view2 = new Int8Array(buffer); //Put value of 32bits view1.setInt32(0, 0x76543210); //prints the 32bit value document.write(view1.getInt32(0).toString(16) + "<br>"); //prints only 8bit value document.write(view1.getInt8(0).toString(16) + "<br>"); document.write(view2[0].toString(16)); </script> |
Output:
76543210 76 76
Properties :
- ArrayBuffer.byteLength: The byteLength property returns the length of the buffer in bytes.
- ArrayBuffer.prototype: This property allows to add properties to all ArrayBuffer objects.
Methods:
- ArrayBuffer.isView(arg): If arg is one of the ArrayBuffer views (typed array objects or a DataView) then true is returned otherwise, false is returned.
- ArrayBuffer.transfer(oldBuffer [, newByteLength]): The contents from the oldbuffer specified is either truncated or zero-extended by the specified newByteLength and is returned as a new ArrayBuffer.
Instance Methods:
- ArrayBuffer.slice() and ArrayBuffer.prototype.slice(): A new ArrayBuffer is returned whose contents are a copy of this ArrayBuffer’s bytes from begin, inclusive, up to end, exclusive.
Recommended Posts:
- JavaScript | arrayBuffer.byteLength
- JavaScript | ArrayBuffer.isView()
- JavaScript arrayBuffer.slice() property
- JavaScript | arrayBuffer.byteLength property
- How to access an object having spaces in the object's key using JavaScript ?
- How to check a JavaScript Object is a DOM Object ?
- Map vs Object in JavaScript
- How to get a key in a JavaScript object by its value ?
- Object.is( ) in JavaScript
- How to get the first key name of a JavaScript object ?
- How to get the size of a JavaScript object?
- Rename object key in JavaScript
- How to add key/value pair to a JavaScript object?
- Object.values( ) In JavaScript
- Object.freeze( ) in JavaScript
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


