| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView/DataView | [Back] [Original] |
Get to know MDN better
This feature is well established and works across many devices and browser versions. Its been available across browsers since July 2015.
The DataView() constructor creates DataView objects.
// Create an ArrayBuffer with a size in bytes
const buffer = new ArrayBuffer(16);
// Create a couple of views
const view1 = new DataView(buffer);
const view2 = new DataView(buffer, 12, 4); // From byte 12 for the next 4 bytes
view1.setInt8(12, 42); // Put 42 in slot 12
console.log(view2.getInt8(0));
// Expected output: 42
new DataView(buffer)
new DataView(buffer, byteOffset)
new DataView(buffer, byteOffset, byteLength)
Note:
DataView() can only be constructed with new. Attempting to call it without new throws a TypeError.
bufferAn existing ArrayBuffer or SharedArrayBuffer to use as
the storage backing the new DataView object.
byteOffset OptionalThe 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 OptionalThe number of elements in the byte array. If unspecified, the view's length will match the buffer's length.
A new DataView object representing the specified data buffer.
RangeErrorThrown if the byteOffset or byteLength parameter values result in the view extending past the end of the buffer. In other words, byteOffset + byteLength > buffer.byteLength.
const buffer = new ArrayBuffer(16);
const view = new DataView(buffer, 0);
view.setInt16(1, 42);
view.getInt16(1); // 42
| Specification |
|---|
| ECMAScript 2027 LanguageSpecification # sec-dataview-constructor |
This page was last modified on Jul 10, 2025 by MDN contributors.
DataViewObject/FunctionYour blueprint for a better internet.
Portions of this content are 19982026 by individual mozilla.org contributors. Content available under a Creative Commons license.
| Web Proxy Viewer | New URL | Original Page |