bugl
bugl
HomeLearnPatternsPathsSearchPremium
HomeLearnPatternsPaths
Learn/JavaScript/Objects, Classes, and Advanced Patterns
JavaScript•Objects, Classes, and Advanced Patterns

JavaScript DataView

The DataView Object lets you read and write multiple numeric types in an ArrayBuffer , at any byte offset, with optional control over endianness (byte order).

What Is a DataView?

A DataView is a view on top of an ArrayBuffer .

A DataView does not store data by itself; instead, it lets you interpret the bytes in the buffer as different types:

  • 8-bit, 16-bit, 32-bit integers (signed and unsigned)
  • 32-bit and 64-bit floating point numbers
  • At arbitrary byte offsets in the buffer
  • With configurable little-endian or big-endian order

Use DataView when

  • You have a binary format with fields of different types and sizes.
  • You need to control byte order (endianness).
  • Typed arrays like Uint8Array or Float32Array are not flexible enough.

Use DataView when you work with binary data where you need full control over byte layout and types (for example network packets, file formats, or interoperability with other languages).

Creating a DataView

To create a DataView , you need an ArrayBuffer .

Then you pass the ArrayBuffer to the new DataView() constructor:

Example

// Create a 16 bytes ArrayBuffer
const buffer = new ArrayBuffer(16);
// Create a new DataView
const view = new DataView(buffer);
// Get lengths and offset
let len1 = buffer.byteLength);
let len2 = view.byteLength;
let off1 = view.byteOffset;

You can also create a DataView that starts at a specific offset and has a limited length:

Example

// Create a 16 bytes ArrayBuffer
const buffer = new ArrayBuffer(16);
// Create a new DataView
const view = new DataView(buffer, 4, 8);
// Get lengths and offset
let len1 = buffer.byteLength);
let len2 = view.byteLength;
let off1 = view.byteOffset;

Multiple DataView and typed array views can share the same ArrayBuffer.

Reading and Writing DataView Values

DataView uses getXxx() and setXxx() methods to read and write values:

  • getInt8(), setInt8()
  • getUint16(), setUint16()
  • getInt32(), setInt32()
  • getFloat32(), setFloat32()
  • and more

Example

getInt32() and setInt32()

const buffer = new ArrayBuffer(8);
const view = new DataView(buffer);
// Write a 32-bit signed integer at byte offset 0
view.setInt32(0, 123456);
// Read it back
const value = view.getInt32(0);
console.log(value); // 123456

Example

getFloat64() and setFloat64()

const buffer = new ArrayBuffer(16);
const view = new DataView(buffer);
view.setFloat64(0, Math.PI);
view.setFloat64(8, Math.E);
const pi = view.getFloat64(0);
const e = view.getFloat64(8);
console.log("PI:", pi);
console.log("E:", e);

Endianness (Byte Order)

Many binary formats specify whether multi-byte numbers are stored as little-endian or big-endian .

  • Little-endian: Least significant byte first (most common on PCs).
  • Big-endian: Most significant byte first (common in some protocols).

Most DataView numeric methods have an optional littleEndian parameter:

  • true = little-endian
  • false (or omitted) = big-endian

Example: Writing with Little-Endian and Big-Endian

const buffer = new ArrayBuffer(4);
const view = new DataView(buffer);
// Same number, two different byte orders:
view.setUint32(0, 0x12345678, true); // little-endian
console.log(new Uint8Array(buffer));
// Example output: Uint8Array(4) [120, 86, 52, 18] (0x78, 0x56, 0x34, 0x12)
// Overwrite with big-endian
view.setUint32(0, 0x12345678, false); // big-endian
console.log(new Uint8Array(buffer));
// Example output: Uint8Array(4) [18, 52, 86, 120] (0x12, 0x34, 0x56, 0x78)

Mixed Types in One Buffer

One of the main advantages of DataView is that you can mix different types in a single buffer. For example:

  • 1 byte for a flag
  • 2 bytes for a length
  • 4 bytes for a float

Example: Mixed Types

const buffer = new ArrayBuffer(12);
const view = new DataView(buffer);
// Byte 0: a flag (0 or 1)
view.setUint8(0, 1);
// Bytes 1-2: a 16-bit length (little-endian)
view.setUint16(1, 500, true);
// Bytes 4-11: a 64-bit float (skip byte 3 for alignment)
view.setFloat64(4, 3.14159, true);
// Read the values back
const flag = view.getUint8(0);
const length = view.getUint16(1, true);
const value = view.getFloat64(4, true);
console.log("Flag:", flag);
console.log("Length:", length);
console.log("Value:", value);

Summary

  • DataView is a flexible way to read and write multiple numeric types in an ArrayBuffer .
  • Use getXxx() and setXxx() methods with a byte offset (and optional endianness).
  • Great for binary protocols, file formats, and interoperability with other languages.
  • Combine DataView with typed arrays to inspect individual bytes easily.

Common Use Cases

  • Parsing binary file formats (images, audio, custom formats).
  • Implementing binary network protocols on top of WebSocket or WebRTC.
  • Interop with WebAssembly or native code where layout matters.
  • Re-implementing C/C++ style structs and unions in JavaScript.

Constructor

SyntaxDescription
new DataView(buffer)View on entire ArrayBuffer .
new DataView(buffer, byteOffset)View starting at byteOffset to the end.
new DataView(buffer, byteOffset, byteLength)View of length byteLength starting at byteOffset .

Properties

PropertyDescription
dataView.bufferThe underlying ArrayBuffer .
dataView.byteLengthNumber of bytes in this view.
dataView.byteOffsetOffset (in bytes) of this view from the start of the buffer.

Read Methods (get)

MethodDescription
getInt8(byteOffset)Reads an 8-bit signed integer.
getUint8(byteOffset)Reads an 8-bit unsigned integer.
getInt16(byteOffset, littleEndian?)Reads a 16-bit signed integer.
getUint16(byteOffset, littleEndian?)Reads a 16-bit unsigned integer.
getInt32(byteOffset, littleEndian?)Reads a 32-bit signed integer.
getUint32(byteOffset, littleEndian?)Reads a 32-bit unsigned integer.
getFloat32(byteOffset, littleEndian?)Reads a 32-bit floating point number.
getFloat64(byteOffset, littleEndian?)Reads a 64-bit floating point number.
getBigInt64(byteOffset, littleEndian?)Reads a 64-bit signed integer as BigInt .
getBigUint64(byteOffset, littleEndian?)Reads a 64-bit unsigned integer as BigInt .

Write Methods (set)

MethodDescription
setInt8(byteOffset, value)Writes an 8-bit signed integer.
setUint8(byteOffset, value)Writes an 8-bit unsigned integer.
setInt16(byteOffset, value, littleEndian?)Writes a 16-bit signed integer.
setUint16(byteOffset, value, littleEndian?)Writes a 16-bit unsigned integer.
setInt32(byteOffset, value, littleEndian?)Writes a 32-bit signed integer.
setUint32(byteOffset, value, littleEndian?)Writes a 32-bit unsigned integer.
setFloat32(byteOffset, value, littleEndian?)Writes a 32-bit floating point number.
setFloat64(byteOffset, value, littleEndian?)Writes a 64-bit floating point number.
setBigInt64(byteOffset, value, littleEndian?)Writes a 64-bit signed integer BigInt .
setBigUint64(byteOffset, value, littleEndian?)Writes a 64-bit unsigned integer BigInt .

Learn More

Typed Array Methods

Typed Array Reference

Array Buffers

Atomics

Previous

JavaScript ArrayBuffer

Next

JavaScript Atomics

This chapter

Overview
123

Lessons

539m

Read time

1. JavaScript Objects2. JavaScript Temporal Tutorial3. JavaScript Loops4. JavaScript Advanced Functions5. JavaScript Objects6. JavaScript Temporal Intro7. JavaScript Iterables8. JavaScript Function Definitions9. JavaScript Object Properties10. JavaScript Temporal vs Date11. JavaScript Iterators12. JavaScript Callbacks13. JavaScript Object Methods14. JavaScript Temporal Duration15. JavaScript Generators16. The JavaScript this Keyword17. JavaScript this Keyword18. JavaScript Temporal Instant19. JavaScript Function call()20. JavaScript Display Objects21. JavaScript PlainDateTime22. JavaScript Function apply()23. JavaScript Object Constructors24. JavaScript Temporal PlainDate25. JavaScript Function bind()26. JavaScript PlainYearMonth27. Self-Invoking Functions28. JavaScript PlainMonthDay29. JavaScript Closures30. JavaScript Temporal PlainTime31. JavaScript Objects - Advanced32. JavaScript ZonedDateTime33. JavaScript Object Definitions34. JavaScript Temporal Now35. this in JavaScript Objects36. JS Temporal Arithmetic37. JavaScript Object Iterations38. JS Temporal Differences39. JavaScript Object Accessors40. JS Temporal Compare41. JavaScript Object Management42. JavaScript Temporal Conversion43. JavaScript Object Protection44. Temporal Display Formats45. JavaScript Object Prototypes46. JavaScript Temporal Mistakes47. JavaScript Classes48. Migrate from Date to Temporal49. JavaScript Class Inheritance50. Temporal Standards51. JavaScript Static Methods52. Asynchronous JavaScript53. Asynchronous Programming54. JavaScript Timeouts55. JavaScript Callbacks56. JavaScript Promises57. JavaScript async and await58. JavaScript fetch API59. Debugging Async JavaScript60. JavaScript Modules61. JavaScript Modules Export62. JavaScript Modules Import63. JS Module Namespace64. JavaScript Dynamic Modules65. JavaScript Meta Programming66. JavaScript Reflect67. JavaScript Proxy68. JavaScript Typed Arrays69. Typed Array Methods70. JavaScript ArrayBuffer71. JavaScript DataView72. JavaScript Atomics73. JavaScript HTML DOM Navigation74. JavaScript HTML DOM Elements (Nodes)75. JavaScript HTML DOM Collections76. JavaScript HTML DOM Node Lists77. JavaScript Window - The Browser Object Model78. JavaScript Window Screen79. JavaScript Window Location80. JavaScript Window History81. JavaScript Window Navigator82. JavaScript Popup Boxes83. JavaScript Timing Events84. JavaScript Cookies85. Web APIs - Introduction86. JavaScript Fetch API87. Web Geolocation API88. Web History API89. Pointer Events API90. Web Storage API91. JavaScript Validation API92. Web Workers API93. AJAX Introduction94. AJAX - The XMLHttpRequest Object95. AJAX - XMLHttpRequest96. AJAX - Server Response97. AJAX XML Example98. AJAX PHP Example99. AJAX ASP Example100. AJAX Database Example101. XML Applications102. JavaScript JSON103. JSON Syntax104. JSON vs XML105. JSON Data Types106. JSON.parse()107. JSON.stringify()108. JSON Object Literals109. JSON Array Literals110. JSON PHP111. JSON HTML112. JSONP113. JavaScript / jQuery DOM Selectors114. JavaScript / jQuery HTML Elements115. JavaScript / jQuery CSS Styles116. JavaScript / jQuery HTML DOM117. JavaScript Graphics118. HTML Canvas119. Plotly.js120. Chart.js121. Google Chart122. D3.js123. JavaScript Interview Preparation

On this page

What Is a DataView?Creating a DataViewReading and Writing DataView ValuesEndianness (Byte Order)Example: Writing with Little-Endian and Big-EndianMixed Types in One BufferExample: Mixed TypesSummaryCommon Use CasesConstructorPropertiesRead Methods (get)Write Methods (set)