DataView
Creates a new DataView providing a low-level interface for reading and writing multiple number types in an ArrayBuffer
Syntax
new DataView(buffer, byteOffset?, byteLength?)Parameters
| Parameter | Type | Description |
|---|---|---|
| buffer | ArrayBuffer | An existing ArrayBuffer to use as the storage |
| byteOffset | number | The offset in bytes from the start of the buffer |
| byteLength | number | The number of elements in the byte array |
Return Value
A new DataView object
Examples
const buffer = new ArrayBuffer(16)
const view = new DataView(buffer)
view.setInt32(0, 42)
console.log(view.getInt32(0)) // 42const buffer = new ArrayBuffer(8)
const view = new DataView(buffer)
view.setFloat64(0, Math.PI)
console.log(view.getFloat64(0)) // 3.141592653589793function parseBinaryHeader(buffer: ArrayBuffer) {
const view = new DataView(buffer)
return {
magic: view.getUint32(0),
version: view.getUint16(4),
flags: view.getUint16(6)
}
}Understanding DataView
The DataView method in JavaScript creates a new DataView providing a low-level interface for reading and writing multiple number types in an ArrayBuffer. It belongs to the DataView object and is one of the most widely used methods for working with dataview values in modern JavaScript and TypeScript applications.
The method signature is new DataView(buffer, byteOffset?, byteLength?). It accepts 3 parameters: buffer, byteOffset, byteLength. When called, it returns a new dataview object. Understanding when and how to use DataView() helps you write more expressive, readable code.
Common use cases for DataView include data transformation, input validation, API response processing, and building reusable utility functions. It works well alongside related methods like dataview-getint32, dataview-setint32, arraybuffer-constructor, enabling you to chain operations together for complex data manipulation pipelines.
Browser support for DataView is excellent across all modern browsers including Chrome, Firefox, Safari, and Edge. It is also fully supported in Node.js and Deno. For older environments, transpilation with Babel or a polyfill may be needed.
Browser Compatibility
Supported in all modern browsers (Chrome, Firefox, Safari, Edge) and Node.js. Part of the ECMAScript standard.
Related Methods
DataView.prototype.getInt32Gets a signed 32-bit integer at the specified byte offset from the start of the DataView
DataView.prototype.setInt32Stores a signed 32-bit integer at the specified byte offset from the start of the DataView
ArrayBufferCreates a new ArrayBuffer of the given length in bytes, with contents initialized to zero
More DataView Methods
Other methods in the DataView object
Related Tools
More DataView Methods
Explore JavaScript Methods
Browse our complete reference of 410 JavaScript methods with syntax, examples, and explanations.