JavaScript DataView getUint32() Method

Beginner
6 min read
Updated: Nov 2026
3 examples
Instance method

Definition

getUint32() reads four bytes from a DataView and returns an unsigned 32-bit whole number from 0 to 4294967295 (about 4.3 billion). File formats and protocols use this type for sizes, IDs, checksums, timestamps in seconds and “magic numbers” that identify a file type. It reads big-endian by default; pass true for little-endian data.

Remember
const view = new DataView(new Uint8Array([0x89, 0x50, 0x4e, 0x47]).buffer);
view.getUint32(0);                 // 2303741511
view.getUint32(0).toString(16);    // "89504e47"
view.getUint32(0, true);           // 1196314761
view.getInt32(0);                  // -1991225785

Syntax

Call getUint32() on a DataView object with a byte position and, if the data is little-endian, true.

JavaScript
dataview.getUint32(byteOffset)
dataview.getUint32(byteOffset, littleEndian)
CallWhat it does
view.getUint32(byteOffset)Reads the 4 bytes starting at byteOffset in big-endian order (most significant byte first) and returns an unsigned whole number (never negative) from 0 to 4294967295.
view.getUint32(byteOffset, true)Reads the same 4 bytes in little-endian order (least significant byte first), the order used by most file formats from PCs, such as WAV and BMP.

Part of the original DataView from ES2015, so every modern browser and runtime has it. Unlike a Uint32Array, a DataView has no alignment rule: getUint32() works at any byte position, even one that is not a multiple of 4.

Library declarations
JavaScript
DataView.prototype.getUint32(byteOffset)
DataView.prototype.getUint32(byteOffset, littleEndian)

// returns a Number: 0 to 4294967295
// reads 4 bytes starting at byteOffset (counted from the start of the view)
// needs byteOffset + 4 <= view.byteLength, else RangeError
// littleEndian: false or left out = big-endian

Parameters

ParameterDescription
byteOffsetWhere to start reading, counted in bytes from the start of the view (not of the underlying buffer). It is converted to a whole number, so 1.9 means 1 and "2" means 2. All 4 bytes must fit: byteOffset + 4 may not be larger than view.byteLength. A negative or too-large offset throws a RangeError.
littleEndianOptional. Leave it out (or pass false) for big-endian: the most significant of the 4 bytes comes first. Pass true for little-endian: the least significant byte comes first. Any truthy value, such as 1, also means little-endian.

Return Value

Return ValueDescription
NumberA whole Number from 0 to 4294967295, built from the 4 bytes at byteOffset. It is never negative.

JavaScript Examples

Example 1 — Turn 4 Bytes Into a Date

Many file formats store time as the number of seconds since 1 January 1970 (UTC) in an unsigned 32-bit field.

JavaScript
const view = new DataView(new ArrayBuffer(4));
view.setUint32(0, 1700000000);   // seconds since 1 January 1970

const seconds = view.getUint32(0);
console.log(seconds);                                  // 1700000000
console.log(new Date(seconds * 1000).toISOString());   // 2023-11-14T22:13:20.000Z
Try It Yourself

How It Works

  1. Time as a count of seconds. Unix time counts seconds since 1 January 1970 (UTC); here the 4 bytes hold 1700000000.
  2. Read it back. getUint32(0) returns the 4 bytes as the number 1700000000.
  3. Make it a date. JavaScript dates use milliseconds, so the seconds are multiplied by 1000 before new Date(); toISOString() prints the UTC time.

Example 2 — Recognise PNG and PDF Files by Their Magic Number

Read the first 4 bytes of a file as one number and compare it with known signatures.

JavaScript
function fileType(bytes) {
  const view = new DataView(bytes.buffer);
  const magic = view.getUint32(0);
  if (magic === 0x89504e47) return "PNG image";
  if (magic === 0x25504446) return "PDF document";
  return "unknown";
}

console.log(fileType(new Uint8Array([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a])));   // PNG image
console.log(fileType(new Uint8Array([0x25, 0x50, 0x44, 0x46, 0x2d])));         // PDF document
console.log(fileType(new Uint8Array([0x00, 0x01, 0x02, 0x03])));               // unknown
Try It Yourself

How It Works

  1. Files announce their type. Many formats start with fixed bytes called a magic number: PNG files begin 89 50 4e 47, and PDF files begin with "%PDF" (25 50 44 46).
  2. Compare four bytes at once. getUint32(0) turns the first 4 bytes into one number, so a single === comparison with 0x89504e47 does the job.
  3. Anything else is unknown. The last array starts with different bytes, so neither check matches.

Example 3 — Read the Size Fields of a BMP Header

A BMP file stores the image width and height as 32-bit little-endian numbers. Read them from 8 header bytes.

JavaScript
const header = new Uint8Array([0x80, 0x02, 0x00, 0x00, 0xe0, 0x01, 0x00, 0x00]);
const view = new DataView(header.buffer);

const width = view.getUint32(0, true);
const height = view.getUint32(4, true);
console.log(width + " x " + height);                          // 640 x 480
console.log("big-endian would say " + view.getUint32(0));   // big-endian would say 2147614720
Try It Yourself

How It Works

  1. Part of a bitmap header. These 8 bytes are the width and height fields of a BMP header, one after the other.
  2. Read with true. getUint32(0, true) and getUint32(4, true) give 640 and 480, a classic VGA size.
  3. The wrong order is obvious. Read big-endian, the same width bytes become 2147614720, a clear sign that the byte order is wrong.

Browser & Runtime Support

getUint32() has been part of JavaScript since ES2015, when DataView itself was standardised.

✓ ES2015 · Baseline

DataView.prototype.getUint32()

Works in every modern browser and in Node.js, Deno and Bun. It runs on a normal ArrayBuffer, so no special page setup is needed.

100% Modern browser support
Google Chrome Modern versions · Desktop & Mobile
Full support
Mozilla Firefox Modern versions · Desktop & Mobile
Full support
Apple Safari Modern versions · macOS & iOS
Full support
Microsoft Edge Modern versions · Chromium
Full support
Opera Modern versions
Full support
Samsung Internet Modern versions · Android
Full support
Internet Explorer Retired browser · not tested here
Legacy
Node.js Current releases · checked on Node 24
Supported
Deno Current releases
Supported
Bun Current releases
Supported
getUint32() All modern engines

Bottom line: Safe to use without a fallback. It also works on a SharedArrayBuffer, but a normal ArrayBuffer is all you need.

Frequently Asked Questions

A whole Number from 0 to 4294967295, read from the four bytes at the offset.
Read the seconds with view.getUint32(offset) and pass seconds * 1000 to new Date(), because JavaScript dates count milliseconds.
Signatures such as 0x89504e47 have the top bit set. getInt32() would return a negative number, while getUint32() matches the hex constant directly.
No, it returns a normal Number. The largest result, 4294967295, is far below Number.MAX_SAFE_INTEGER.
The four bytes are read in reverse, so the number is wrong: a width of 640 shows up as 2147614720.

Next: getUint8()

Read a single byte from 0 to 255: colours, flags and hex dumps.

getUint8() tutorial →

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.

11 people found this page helpful