DataView() constructor
The DataView() constructor creates DataView objects.
Try it
Syntax
Parameters
buffer-
An existing
ArrayBufferorSharedArrayBufferto use as the storage backing the newDataViewobject. byteOffsetOptional-
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.
byteLengthOptional-
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.
Exceptions
RangeError-
Thrown if the
byteOffsetorbyteLengthparameter values result in the view extending past the end of the buffer.For example, if the buffer is 16 bytes long, the
byteOffsetis 8, and thebyteLengthis 10, this error is thrown because the resulting view tries to extend 2 bytes past the total length of the buffer.
Examples
Using DataView
js
const buffer = new ArrayBuffer(16);
const view = new DataView(buffer, 0);
view.setInt16(1, 42);
view.getInt16(1); // 42
Specifications
| Specification |
|---|
| ECMAScript Language Specification # sec-dataview-constructor |
Browser compatibility
BCD tables only load in the browser