JavaScript DataView setUint32() Method

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

Definition

setUint32() writes an unsigned 32-bit whole number (0 to 4294967295) into four bytes of a DataView. Pass the byte offset, the value and, for little-endian formats, true. One call replaces four separate byte writes, and the byte order argument decides which byte goes where.

Remember
const view = new DataView(new ArrayBuffer(4));
view.setUint32(0, 0xdeadbeef);         // undefined
view.getUint8(0).toString(16);         // "de"
view.setUint32(0, 0xdeadbeef, true);
view.getUint8(0).toString(16);         // "ef"
view.getUint32(0, true);               // 3735928559

Syntax

Call setUint32() on a DataView object with a byte position and the value to write, plus true if you need little-endian order.

JavaScript
dataview.setUint32(byteOffset, value)
dataview.setUint32(byteOffset, value, littleEndian)
CallWhat it does
view.setUint32(byteOffset, value)Converts value to a unsigned 32-bit integer (0 to 4294967295) and writes it into the 4 bytes starting at byteOffset, big-endian. Returns undefined.
view.setUint32(byteOffset, value, true)Writes the same 4 bytes in little-endian order, so the least significant byte lands at byteOffset.

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

Library declarations
JavaScript
DataView.prototype.setUint32(byteOffset, value)
DataView.prototype.setUint32(byteOffset, value, littleEndian)

// returns undefined; writes 4 bytes starting at byteOffset
// value is converted to a unsigned 32-bit integer
// needs byteOffset + 4 <= view.byteLength, else RangeError
// littleEndian: false or left out = big-endian

Parameters

ParameterDescription
byteOffsetWhere to start writing, 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.
valueThe number to store. It is converted to a Number, the fraction is cut off (3.9 becomes 3, -3.9 becomes -3), and the result wraps around modulo 232 into 0 to 4294967295. NaN and Infinity are stored as 0.
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
undefinedsetUint32() gives nothing back. Its result is the 4 bytes it changed inside the buffer; read them back with getUint32().

JavaScript Examples

Example 1 — Write Width and Height Into a BMP-Style Header

Write a Full HD image size as two 32-bit little-endian numbers, the way a BMP header stores them.

JavaScript
const header = new DataView(new ArrayBuffer(8));
header.setUint32(0, 1920, true);   // width
header.setUint32(4, 1080, true);   // height

const hex = Array.from(new Uint8Array(header.buffer), function (b) {
  return b.toString(16).padStart(2, "0");
}).join(" ");
console.log(hex);   // 80 07 00 00 38 04 00 00
Try It Yourself

How It Works

  1. Two size fields. A bitmap header keeps width and height as 4-byte little-endian numbers, one after the other.
  2. Write each with true. setUint32(0, 1920, true) puts the low byte, 80, first; 1080 starts with 38.
  3. Check the bytes. The hex dump 80 07 00 00 38 04 00 00 is exactly what an image program expects for a 1920 × 1080 picture.

Example 2 — Split a 32-bit Number Into an IPv4 Address

Databases and logs often keep an IPv4 address as one number. Write it with setUint32() and read the four parts back.

JavaScript
const view = new DataView(new ArrayBuffer(4));
view.setUint32(0, 3232235786);   // the address as one number

const parts = [];
for (let i = 0; i < 4; i++) {
  parts.push(view.getUint8(i));
}
console.log(parts.join("."));   // 192.168.1.10
Try It Yourself

How It Works

  1. An address stored as a number. The 32-bit number 3232235786 holds all four parts of an IPv4 address.
  2. Big-endian is network order. setUint32() without true writes the most significant byte first, which is the order the address parts are written in.
  3. Split it back into parts. Reading the four bytes with getUint8() and joining them with dots gives 192.168.1.10.

Example 3 — Values Above 4294967295 and Below Zero

See what setUint32() stores for the maximum value, values just past it, a negative number and Infinity.

JavaScript
const view = new DataView(new ArrayBuffer(4));
const values = [4294967295, 4294967296, 4294967301, -1, Infinity];

values.forEach(function (v) {
  view.setUint32(0, v);
  console.log(String(v) + " -> " + view.getUint32(0));
});
Try It Yourself

How It Works

  1. The top value fits. 4294967295 (2 to the power 32, minus 1) is the largest unsigned 32-bit number and is stored unchanged.
  2. Bigger values wrap. 4294967296 becomes 0 and 4294967301 becomes 5: only the remainder after dividing by 2 to the power 32 is kept.
  3. Negative and infinite input. -1 wraps to 4294967295, and Infinity is stored as 0.

Browser & Runtime Support

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

✓ ES2015 · Baseline

DataView.prototype.setUint32()

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
setUint32() 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

It converts a number to an unsigned 32-bit value from 0 to 4294967295 and writes it into four bytes at the given offset.
Use setBigUint64() with a BigInt, which has 8 bytes of room. setUint32() would wrap the value around.
true selects little-endian order, which writes the least significant byte first. That is correct for formats like BMP and WAV.
4294967295. Negative values wrap around by 2 to the power 32, so -1 becomes the largest unsigned value.
Yes. Write the address as one number in big-endian order; the four bytes are then the four parts of the address.

Next: setUint8()

Finish with the simplest setter: write one byte from 0 to 255.

setUint8() 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