JavaScript DataView

Beginner
9 min read
Updated: Nov 2026
3 examples
Binary data

What is a DataView?

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.

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.

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:

QuestionTyped arrayDataView
Number typesOne type for the whole arrayAny mix: each call picks its own type
AlignmentA Uint32Array must start at a multiple of 4, a Float64Array at a multiple of 8No alignment rule: read 4 bytes starting at byte 3 if the format says so
Byte orderAlways 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 endReading gives undefined, writing is ignoredThrows a RangeError
Best forLong lists of one type: pixels, audio samples, vertex dataFile headers, network packets, any format with mixed fields

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.

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 nameMeaningExample
get / setRead a number and return it, or write a number and return undefinedgetUint16(), setUint16()
Int / UintA whole number that can be negative, or an unsigned one that starts at 0getInt32(), getUint32()
8, 16, 32, 64The size in bits; divide by 8 for the number of bytes the call usessetInt8() uses 1 byte, setInt16() uses 2
BigA 64-bit whole number handled as a BigInt, such as 42ngetBigInt64(), setBigUint64()
FloatA number with a fraction in 16, 32 or 64 bitsgetFloat32(), setFloat64()

Errors and Wrap-Around

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.

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.

JavaScript
const view = new DataView(new ArrayBuffer(4));
view.setUint16(0, 0x1234);         // big-endian, the default
view.setUint16(2, 0x1234, true);   // little-endian

const hex = Array.from(new Uint8Array(view.buffer), function (b) {
  return b.toString(16).padStart(2, "0");
}).join(" ");
console.log(hex);                       // 12 34 34 12
console.log(view.getUint16(0, true));   // 13330
console.log(view.getUint16(2, true));   // 4660
Try It Yourself

How It Works

  1. 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.
  2. The same number, two layouts. The bytes show 12 34 for the default big-endian write and 34 12 for the little-endian one.
  3. 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.

JavaScript
function encode(type, id, temp) {
  const view = new DataView(new ArrayBuffer(7));
  view.setUint8(0, type);
  view.setUint16(1, id);
  view.setFloat32(3, temp);
  return view.buffer;
}

function decode(buffer) {
  const view = new DataView(buffer);
  return "type=" + view.getUint8(0) + " id=" + view.getUint16(1) + " temp=" + view.getFloat32(3);
}

const packet = encode(3, 1025, 36.5);
console.log(packet.byteLength);   // 7
console.log(decode(packet));      // type=3 id=1025 temp=36.5
Try It Yourself

How It Works

  1. 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.
  2. encode() writes the fields. setUint8(), setUint16() and setFloat32() fill offsets 0, 1 and 3, and the function returns the 7-byte buffer.
  3. 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.

JavaScript
const buffer = new ArrayBuffer(6);
const view = new DataView(buffer, 2);

view.setUint8(0, 300);
console.log(new Uint8Array(buffer)[2]);   // 44

try {
  view.getUint32(1);
} catch (e) {
  console.log(e.name);                    // RangeError
}
console.log(view.byteOffset + " " + view.byteLength);   // 2 4
Try It Yourself

How It Works

  1. Offsets are relative to the view. The view starts at byte 2 of the buffer, so setUint8(0, ...) changes buffer byte 2.
  2. 300 does not fit in a byte. Only the low 8 bits are kept: 300 - 256 = 44. DataView does not throw for values that are too big.
  3. Positions are checked strictly. The view has 4 bytes, so a 4-byte read at offset 1 would need bytes 1 to 4 and throws a RangeError.

DataView Method Groups

DataView has twenty-two methods: eleven getters and the eleven matching setters. Each one has a full tutorial with examples you can run.

GroupMethodsUse them to
Read whole numbers (get)getInt8(), getUint8(), getInt16(), getUint16(), getInt32(), getUint32()decode flags, lengths, ports, counters and pixel values from a binary file or packet
Read big numbers (get)getBigInt64(), getBigUint64()read 64-bit timestamps, file sizes and ids exactly, beyond the safe Number range
Read decimals (get)getFloat16(), getFloat32(), getFloat64()decode sensor values, audio samples, coordinates and machine-learning weights
Write whole numbers (set)setInt8(), setUint8(), setInt16(), setUint16(), setInt32(), setUint32()build headers, length fields and colour values byte by byte
Write big numbers (set)setBigInt64(), setBigUint64()store 64-bit timestamps, hashes and large counters without losing digits
Write decimals (set)setFloat16(), setFloat32(), setFloat64()save measurements in 2, 4 or 8 bytes, depending on the precision you need

Full list with one-line descriptions: DataView Methods index.

Frequently Asked Questions

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.

Browse every DataView method

Open the index for getUint8(), setUint16(), getFloat32(), setBigInt64(), and the rest, with a type-size table.

DataView Methods index →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

16 people found this page helpful