DataView
The DataView global object is a view that reads and writes numbers of different sizes at any byte offset in a binary buffer, with explicit control over byte order. Each method names the number type it handles, and the multibyte ones take a flag that picks little-endian or big-endian order:
> const view = new DataView(new ArrayBuffer(2));
undefined
> view.setUint16(0, 513);
undefined
> view.getUint8(0)
2
> view.getUint16(0, true)
258
The setter stores 513 big-endian, as the bytes 02 01. Reading the first byte alone gives 2, and reading both bytes as little-endian gives 258. Big-endian is the default whenever the flag is missing or false.
A DataView holds no bytes of its own. It wraps an ArrayBuffer or a SharedArrayBuffer, and every write goes straight into that memory. Typed arrays share the same buffers but read one number type in the platform’s byte order, so a DataView fits file formats and network protocols that mix types.
.getFloat16() and .setFloat16() were added in ES2025 and are Baseline Newly available since 2025, and supported in Node.js 24 and later. The rest of DataView is Baseline Widely available.
Syntax
The DataView constructor takes a buffer and an optional window into it:
new DataView(<buffer>[, <byteOffset>[, <byteLength>]])
| Parameter | Description |
|---|---|
buffer |
The ArrayBuffer or SharedArrayBuffer to view. Any other value throws a TypeError |
byteOffset |
An optional position in the buffer where the view starts. Defaults to 0, and a value past the end of the buffer throws a RangeError |
byteLength |
An optional number of bytes the view covers. A window that runs past the end of the buffer throws a RangeError. Defaults to the rest of the buffer, and a resizable buffer’s view then tracks the buffer’s size as it changes (ES2024) |
The constructor returns a new DataView. The new keyword is required, and calling DataView() without it throws a TypeError.
Key Instance Methods and Properties
Every getter takes a byte offset, counted from the start of the view, plus an optional little-endian flag for multibyte types. Every setter takes the offset, the value, and the same flag, and returns undefined:
| Member | Kind | Description |
|---|---|---|
.buffer |
Instance property | Returns the buffer the view reads and writes |
.byteOffset, .byteLength |
Instance property | Return where the view starts in the buffer and how many bytes it covers |
.getInt8(), .getUint8() |
Instance method | Read one byte as a signed or unsigned integer |
.getInt16(), .getUint16() |
Instance method | Read two bytes as a signed or unsigned integer |
.getInt32(), .getUint32() |
Instance method | Read four bytes as a signed or unsigned integer |
.getFloat16() |
Instance method | Reads two bytes as a half-precision floating-point number (ES2025) |
.getFloat32(), .getFloat64() |
Instance method | Read four or eight bytes as a floating-point number |
.getBigInt64(), .getBigUint64() |
Instance method | Read eight bytes as a signed or unsigned 64-bit integer and return a bigint |
.setInt8(), .setUint8(), .setInt16(), .setUint16(), .setInt32(), .setUint32() |
Instance method | Write an integer of the matching size |
.setFloat16() |
Instance method | Writes a number as a half-precision float, rounding it to fit (ES2025) |
.setFloat32(), .setFloat64() |
Instance method | Write a number as a four-byte or eight-byte float |
.setBigInt64(), .setBigUint64() |
Instance method | Write a bigint as a signed or unsigned 64-bit integer |
Examples
A weather station sends seven-byte packets: a one-byte sensor ID, a little-endian 32-bit float temperature, and a little-endian 16-bit air pressure reading. The float starts at byte 1, an offset a Float32Array can’t start at, but a DataView reads it directly:
> const packet = Uint8Array.from([7, 0x00, 0x00, 0xb4, 0x41, 0x2c, 0x01]);
undefined
> const reading = new DataView(packet.buffer);
undefined
> reading.getUint8(0)
7
> reading.getFloat32(1, true)
22.5
> reading.getUint16(5, true)
300
Pick a getter, click a byte to set the offset, and toggle littleEndian to see which bytes of this packet each call reads and what it returns:
A log format stores a four-byte record type followed by a 64-bit timestamp in milliseconds. A view that starts at byte 4 lets the timestamp code count offsets from zero, and the 64-bit methods keep the value exact as a bigint:
> const record = new ArrayBuffer(12);
undefined
> const stamp = new DataView(record, 4);
undefined
> stamp.setBigUint64(0, 1762905600000n);
undefined
> stamp.getBigUint64(0)
1762905600000n
> stamp.byteOffset
4
> stamp.byteLength
8
A model file stores its weights as half-precision floats to save space. Writing 0.1 with .setFloat16() and reading it back shows how much precision two bytes keep:
> const weights = new DataView(new ArrayBuffer(2));
undefined
> weights.setFloat16(0, 0.1);
undefined
> weights.getFloat16(0)
0.0999755859375
Common Gotchas
Offsets are checked against the view, and a read or write that would run past its last byte throws a RangeError:
> const header = new DataView(new ArrayBuffer(4));
undefined
> header.getUint32(2)
Uncaught RangeError: Offset is outside the bounds of the DataView
at DataView.prototype.getUint32 (<anonymous>)
The 64-bit setters convert their value with bigint rules, which reject regular numbers. Passing 5 throws a TypeError, and 5n works:
> const counter = new DataView(new ArrayBuffer(8));
undefined
> counter.setBigInt64(0, 5)
Uncaught TypeError: Cannot convert 5 to a BigInt
at DataView.prototype.setBigInt64 (<anonymous>)
> counter.setBigInt64(0, 5n);
undefined
Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.
By Leodanis Pozo Ramos • Updated Nov. 12, 2026