DataView() constructor
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
TheDataView() constructor createsDataView objects.
In this article
Try it
// Create an ArrayBuffer with a size in bytesconst buffer = new ArrayBuffer(16);// Create a couple of viewsconst view1 = new DataView(buffer);const view2 = new DataView(buffer, 12, 4); // From byte 12 for the next 4 bytesview1.setInt8(12, 42); // Put 42 in slot 12console.log(view2.getInt8(0));// Expected output: 42Syntax
new DataView(buffer)new DataView(buffer, byteOffset)new DataView(buffer, byteOffset, byteLength)Parameters
bufferAn existing
ArrayBufferorSharedArrayBufferto use asthe storage backing the newDataViewobject.byteOffsetOptionalThe offset, in bytes, to the first byte in the above buffer for the new view toreference. If unspecified, the buffer view starts with the first byte.
byteLengthOptionalThe number of elements in the byte array. If unspecified, the view's length willmatch the buffer's length.
Return value
A newDataView object representing the specified data buffer.
Exceptions
RangeErrorThrown if the
byteOffsetorbyteLengthparameter values result in the view extending past the end of the buffer. In other words,byteOffset + byteLength > buffer.byteLength.
Examples
>Using DataView
const buffer = new ArrayBuffer(16);const view = new DataView(buffer, 0);view.setInt16(1, 42);view.getInt16(1); // 42Specifications
| Specification |
|---|
| ECMAScript® 2026 Language Specification> # sec-dataview-constructor> |