The DataView()
constructor is used to create DataView
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 DataView(buffer [, byteOffset [, byteLength]])
Parameters
buffer
- An existing
ArrayBuffer
orSharedArrayBuffer
to use as the storage backing the newDataView
object. byteOffset
Optional- The offset, in bytes, to the first byte in the above buffer for the new view to reference. If unspecified, the buffer view starts with the first byte.
byteLength
Optional- The number of elements in the byte array. If unspecified, the view's length will match the buffer's length.
Return value
A new DataView
object representing the specified data buffer. (That probably wasn't a very helpful description.)
You can think of the returned object as an "interpreter" of the array buffer of bytes — it knows how to convert numbers to fit within the buffer correctly, both when reading and writing to it. This means handling integer and float conversion, endianness, and other details of representing numbers in binary form.
Exceptions
RangeError
-
Thrown if the
byteOffset
orbyteLength
parameter values result in the view extending past the end of the buffer.For example, if the buffer is 16 bytes long, the
byteOffset
is 8, and thebyteLength
is 10, this error is thrown because the resulting view tries to extend 2 bytes past the total length of the buffer.
Examples
Using DataView
var buffer = new ArrayBuffer(16); var view = new DataView(buffer, 0); view.setInt16(1, 42); view.getInt16(1); // 42
Specifications
Specification |
---|
ECMAScript (ECMA-262) The definition of 'DataView constructor' in that specification. |
Browser compatibility
Desktop | Mobile | Server | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
DataView() constructor | Chrome Full support 9 | Edge Full support 12 | Firefox Full support 15 | IE Full support 10 | Opera Full support 12.1 | Safari Full support 5.1 | WebView Android Full support 4 | Chrome Android Full support 18 | Firefox Android Full support 15 | Opera Android Full support 12.1 | Safari iOS Full support 4.2 | Samsung Internet Android Full support 1.0 | nodejs Full support 0.10 |
DataView() without new throws | Chrome Full support 11 | Edge Full support 13 | Firefox Full support 40 | 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 40 | Opera Android Full support 14 | Safari iOS Full support 5.1 | Samsung Internet Android Full support 1.0 | nodejs Full support 0.10 |
SharedArrayBuffer accepted as buffer | Chrome
Full support
68
| Edge Full support 79 | Firefox
Full support
79
| IE No support No | Opera No support No | Safari No support 10.1 — 11 | WebView Android
No support
60 — 63
| Chrome Android
No support
60 — 63
| Firefox Android
Full support
57
| Opera Android No support No | Safari iOS No support 10.3 — 11 | Samsung Internet Android
No support
No
| nodejs Full support 8.10.0 |
Legend
- Full support
- Full support
- No support
- No support
- See implementation notes.
- See implementation notes.
- User must explicitly enable this feature.
- User must explicitly enable this feature.