A DataView is a window onto the raw bytes of an ArrayBuffer. Through it you can read and write numbers of any size, at any byte position: a 1-byte flag, then a 2-byte length, then a 4-byte float, all in the same piece of memory.
That is exactly what binary formats look like. Image headers, network packets, audio files and game saves pack different number types right after each other. DataView lets JavaScript take such data apart and build it, and it lets you choose the byte order for every single number.
A DataView holds no data of its own.
All bytes live in the buffer. Several views, and typed arrays, can share one buffer, and a change through one of them is visible through all the others.
Syntax
Creating a DataView
Pass a buffer and, optionally, where the view starts (byteOffset) and how many bytes it covers (byteLength):
JavaScript
new DataView(buffer)
new DataView(buffer, byteOffset)
new DataView(buffer, byteOffset, byteLength)
JavaScript
const buffer = new ArrayBuffer(8);
const whole = new DataView(buffer);
// a window on bytes 2, 3, 4 and 5 only
const part = new DataView(buffer, 2, 4);
whole.byteLength; // 8
part.byteOffset; // 2
part.byteLength; // 4
part.buffer === buffer; // true
new DataView(buffer, 9); // RangeError
DataView(buffer); // TypeError
The buffer must be an ArrayBuffer or a SharedArrayBuffer; anything else throws a TypeError, and so does calling DataView() without new. A start or length that does not fit inside the buffer throws a RangeError. Every method then counts its byte offset from the start of the view, not from the start of the buffer.
Compare
DataView vs Typed Arrays
A typed array such as Uint16Array also looks at a buffer, but it treats every element as the same type. The differences decide which one to use:
Question
Typed array
DataView
Number types
One type for the whole array
Any mix: each call picks its own type
Alignment
A Uint32Array must start at a multiple of 4, a Float64Array at a multiple of 8
No alignment rule: read 4 bytes starting at byte 3 if the format says so
Byte order
Always the order of the device (little-endian on almost every computer and phone)
Your choice on every call; big-endian unless you pass true
Past the end
Reading gives undefined, writing is ignored
Throws a RangeError
Best for
Long lists of one type: pixels, audio samples, vertex data
File headers, network packets, any format with mixed fields
Byte order
Endianness: Which Byte Comes First?
A number that needs more than one byte must be split up, and there are two ways to order the pieces. Big-endian writes the most significant byte first, like we write digits. Little-endian writes the least significant byte first. Network protocols and PNG files use big-endian; WAV, BMP and most CPUs use little-endian.
JavaScript
const view = new DataView(new ArrayBuffer(2));
// big-endian is the default: the most significant byte goes first
view.setUint16(0, 0x1234);
view.getUint8(0).toString(16); // "12"
// pass true for little-endian: the least significant byte goes first
view.setUint16(0, 0x1234, true);
view.getUint8(0).toString(16); // "34"
view.getUint16(0, true).toString(16); // "1234"
Every method that handles 2 or more bytes takes a last littleEndian argument. Leave it out (or pass false) for big-endian, pass true for little-endian. The 1-byte methods getInt8(), getUint8(), setInt8() and setUint8() have no such argument, because a single byte has nothing to reorder. Reading with the wrong order never throws; it silently gives a different number.
Names
The get/set Naming Rule
All twenty-two method names are built from the same parts, so once you can read one name you can read them all:
Part of the name
Meaning
Example
get / set
Read a number and return it, or write a number and return undefined
DataView is strict about positions but relaxed about values:
JavaScript
const view = new DataView(new ArrayBuffer(4));
// too-big values wrap around instead of throwing
view.setUint8(0, 256);
view.getUint8(0); // 0
view.setInt8(1, 200);
view.getInt8(1); // -56
// reading or writing past the end throws
view.getUint16(3); // RangeError
view.getUint8(-1); // RangeError
view.setBigInt64(0, 1n); // RangeError
The byte offset plus the size of the type must fit inside the view’s byteLength; otherwise you get a RangeError. A negative offset is a RangeError too.
Whole-number setters keep only the bits that fit, so 256 written as one unsigned byte becomes 0 and 200 written as a signed byte becomes -56.
Float setters round, and values beyond the range become Infinity.
The Big setters need a BigInt; passing a plain Number throws a TypeError.
Code
JavaScript Examples
Three short programs that run in any modern browser tab and in Node.js.
Example 1 — Write the Same Number in Both Byte Orders
Write 0x1234 twice into a 4-byte buffer, once big-endian and once little-endian, then look at the bytes.
One buffer, one view.new DataView(new ArrayBuffer(4)) gives four empty bytes and a view that can read or write them in any way.
The same number, two layouts. The bytes show 12 34 for the default big-endian write and 34 12 for the little-endian one.
Reading with the wrong order. Reading the first pair as little-endian gives 13330 (0x3412) instead of 4660 (0x1234): no error, just a different number.
Example 2 — Pack Three Fields Into Seven Bytes
Write a tiny binary format with an encode and a decode function: a 1-byte type, a 2-byte id and a 4-byte float temperature.
Three types, no gaps. The format puts a byte, a 16-bit integer and a 32-bit float right after each other, which only a DataView can handle in one view.
encode() writes the fields.setUint8(), setUint16() and setFloat32() fill offsets 0, 1 and 3, and the function returns the 7-byte buffer.
decode() reads them back. The matching getters at the same offsets recover 3, 1025 and 36.5: the packet survives the round trip.
Example 3 — Offsets, Wrap-Around and RangeError
Create a view that starts at byte 2 of a 6-byte buffer, write a value that is too big, and read past the end.
A DataView is an object that reads and writes numbers of different types at any byte position of an ArrayBuffer or SharedArrayBuffer, with a byte order you choose.
Use DataView when the data mixes number types or has fields at odd positions, as in file headers and network packets. Use a typed array for long lists of one type.
The most significant byte is stored first. DataView methods use big-endian unless you pass true as the littleEndian argument.
No. It only looks at the bytes of the buffer, so changes through the DataView are visible in every other view of the same buffer.
The offset plus the size of the type goes past the end of the view, or the offset is negative. Check byteLength and remember that offsets count from the start of the view.