JavaScript DataView setInt32() Method

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

Definition

setInt32() writes a signed 32-bit whole number (-2147483648 to 2147483647) into four bytes of a DataView. The arguments are the byte offset, the value and an optional true for little-endian order. Like all integer setters it never complains about the value: decimals are cut off, NaN becomes 0, and numbers that are too large wrap around.

Remember
const view = new DataView(new ArrayBuffer(4));
view.setInt32(0, -1);       // undefined
view.getUint32(0);          // 4294967295
view.setInt32(0, 7.99);
view.getInt32(0);           // 7
view.setInt32(1, 5);        // RangeError

Syntax

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

JavaScript
dataview.setInt32(byteOffset, value)
dataview.setInt32(byteOffset, value, littleEndian)
CallWhat it does
view.setInt32(byteOffset, value)Converts value to a signed 32-bit integer (-2147483648 to 2147483647) and writes it into the 4 bytes starting at byteOffset, big-endian. Returns undefined.
view.setInt32(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 Int32Array, a DataView has no alignment rule: setInt32() works at any byte position, even one that is not a multiple of 4.

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

// returns undefined; writes 4 bytes starting at byteOffset
// value is converted to a signed 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 -2147483648 to 2147483647. 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
undefinedsetInt32() gives nothing back. Its result is the 4 bytes it changed inside the buffer; read them back with getInt32().

JavaScript Examples

Example 1 — Write Signed Amounts in Little-Endian Order

Save a payment and a deposit (in cents) as signed 32-bit little-endian numbers and inspect the bytes.

JavaScript
const amounts = [-2599, 150000];
const view = new DataView(new ArrayBuffer(8));

amounts.forEach(function (cents, i) {
  view.setInt32(i * 4, cents, true);
});

const hex = Array.from(new Uint8Array(view.buffer), function (b) {
  return b.toString(16).padStart(2, "0");
}).join(" ");
console.log(hex);   // d9 f5 ff ff f0 49 02 00
Try It Yourself

How It Works

  1. Signed amounts in cents. -2599 is a payment of 25.99 and 150000 is a deposit of 1500.00; both fit easily in 32 bits.
  2. Four bytes each, low byte first. Amount i goes to offset i × 4, and true selects little-endian order.
  3. Spot the negative number. The first amount ends in ff ff: in two’s complement, small negative numbers are full of 1 bits at the high end.

Example 2 — How setInt32() Converts Unusual Numbers

Write decimals, a number just past the maximum, NaN and Infinity, then read each one back.

JavaScript
const view = new DataView(new ArrayBuffer(4));
const inputs = [3.99, -3.99, 2147483648, NaN, Infinity];

inputs.forEach(function (x) {
  view.setInt32(0, x);
  console.log(String(x) + " -> " + view.getInt32(0));
});
Try It Yourself

How It Works

  1. Decimals lose their fraction. 3.99 is stored as 3 and -3.99 as -3. If you want rounding, call Math.round() before writing.
  2. Just past the maximum. 2147483648 is one more than the largest signed 32-bit value, so it wraps to -2147483648.
  3. Special values become zero. NaN and Infinity are not whole numbers, so setInt32() writes 0.

Example 3 — A View That Covers Only the Middle Bytes

Create a DataView over bytes 4 to 7 of a 12-byte buffer and write -1 at its offset 0.

JavaScript
const buffer = new ArrayBuffer(12);
const middle = new DataView(buffer, 4, 4);   // only bytes 4 to 7

middle.setInt32(0, -1);

const hex = Array.from(new Uint8Array(buffer), function (b) {
  return b.toString(16).padStart(2, "0");
}).join(" ");
console.log(hex);                 // 00 00 00 00 ff ff ff ff 00 00 00 00
console.log(middle.byteOffset);   // 4
Try It Yourself

How It Works

  1. A window into the buffer. new DataView(buffer, 4, 4) covers only bytes 4 to 7 of the 12-byte buffer; its byteOffset is 4.
  2. Offset 0 means the window start. middle.setInt32(0, -1) writes ff ff ff ff into buffer bytes 4 to 7.
  3. The rest stays zero. The hex dump of the whole buffer shows the change exactly in the middle; the view cannot touch bytes outside its window.

Browser & Runtime Support

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

✓ ES2015 · Baseline

DataView.prototype.setInt32()

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
setInt32() 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 returns undefined after writing four bytes at the offset you gave.
No, it truncates. 3.99 becomes 3 and -3.99 becomes -3. Round first with Math.round() if that is what you need.
It stores 0. NaN, Infinity and -Infinity all become 0 for the integer setters.
To the view. A DataView created with a byteOffset of 4 writes buffer bytes 4 to 7 when you call setInt32(0, value).
IDs are never negative, so setUint32() is the better fit and allows values up to 4294967295. setInt32() makes sense when negative values are possible.

Next: setInt8()

Write a single signed byte and watch values such as 200 wrap around to -56.

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