JavaScript DataView getInt32() Method

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

Definition

getInt32() reads four bytes from a DataView and returns a signed 32-bit whole number from -2147483648 to 2147483647 (about ±2.1 billion). That is enough for most counters, amounts of money in cents, or coordinates stored as whole numbers. The four bytes are read big-endian by default; pass true as the second argument when the data is little-endian.

Remember
const view = new DataView(new Uint8Array([0xff, 0xff, 0xff, 0xfe]).buffer);
view.getInt32(0);         // -2
view.getUint32(0);        // 4294967294
view.getInt32(0, true);   // -16777217
view.getInt32(1);         // RangeError

Syntax

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

JavaScript
dataview.getInt32(byteOffset)
dataview.getInt32(byteOffset, littleEndian)
CallWhat it does
view.getInt32(byteOffset)Reads the 4 bytes starting at byteOffset in big-endian order (most significant byte first) and returns a signed whole number from -2147483648 to 2147483647.
view.getInt32(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 Int32Array, a DataView has no alignment rule: getInt32() works at any byte position, even one that is not a multiple of 4.

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

// returns a Number: -2147483648 to 2147483647
// 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 -2147483648 to 2147483647, built from the 4 bytes at byteOffset using two’s complement for negative values.

JavaScript Examples

Example 1 — Sum a Ledger of Signed Amounts in Cents

A small ledger stores income and spending as signed 32-bit numbers of cents. Read every entry and print the balance.

JavaScript
const ledger = new DataView(new ArrayBuffer(12));
ledger.setInt32(0, 150000);    // salary: +1500.00
ledger.setInt32(4, -2599);     // groceries: -25.99
ledger.setInt32(8, -99900);    // rent: -999.00

let total = 0;
for (let i = 0; i < ledger.byteLength; i += 4) {
  total += ledger.getInt32(i);
}
console.log("balance: " + (total / 100).toFixed(2));   // balance: 475.01
Try It Yourself

How It Works

  1. Money as whole cents. Storing 1500.00 as 150000 cents avoids decimal rounding problems, and a signed type allows spending (negative amounts).
  2. Read every 4-byte entry. The loop steps 4 bytes at a time, and getInt32(i) returns each amount with its sign.
  3. Convert back for display. The total, 47501 cents, divided by 100 and formatted with toFixed(2), prints 475.01.

Example 2 — Decode a GPS Position Stored in Microdegrees

Many GPS devices store degrees multiplied by one million as signed 32-bit little-endian integers. Read a position in London.

JavaScript
const view = new DataView(new ArrayBuffer(8));
view.setInt32(0, 51507400, true);    // latitude x 1,000,000
view.setInt32(4, -127600, true);     // longitude x 1,000,000

const lat = view.getInt32(0, true) / 1e6;
const lon = view.getInt32(4, true) / 1e6;
console.log(lat + ", " + lon);   // 51.5074, -0.1276
Try It Yourself

How It Works

  1. Coordinates as whole numbers. Multiplying 51.5074 by one million gives the integer 51507400, which a 32-bit integer stores exactly.
  2. Little-endian device data. The values were written with true, so they must be read with getInt32(offset, true) as well.
  3. Divide to get degrees. Dividing by 1e6 restores 51.5074 and -0.1276; west of Greenwich the longitude is negative, so the signed type matters.

Example 3 — Watch the Top Bit Turn a Number Negative

Write the largest positive 32-bit value and then one more, and read both with getInt32().

JavaScript
const view = new DataView(new ArrayBuffer(4));
view.setUint32(0, 2147483647);
console.log(view.getInt32(0));   // 2147483647

view.setUint32(0, 2147483648);
console.log(view.getInt32(0));                 // -2147483648
console.log(view.getUint8(0).toString(2));     // 10000000
Try It Yourself

How It Works

  1. The largest positive value. 2147483647 is 0x7fffffff: every bit is 1 except the top one, so getInt32() reads it unchanged.
  2. One more flips the sign. 2147483648 sets only the top bit. getInt32() treats that bit as the minus sign and returns -2147483648.
  3. See the sign bit. The first byte in binary is 10000000: in big-endian order the sign bit is the very first bit of the 4 bytes.

Browser & Runtime Support

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

✓ ES2015 · Baseline

DataView.prototype.getInt32()

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

From -2147483648 to 2147483647, which is -2 to the power 31 up to 2 to the power 31 minus 1.
The ID was stored as an unsigned value above 2147483647. Its top bit is set, which getInt32() treats as a minus sign. Read it with getUint32() instead.
No. Every 32-bit value fits safely in a normal Number, so getInt32() returns a Number. Only the 64-bit methods return BigInt.
Pass true as the second argument: view.getInt32(offset, true).
Four. offset + 4 must not be larger than the view byteLength, otherwise a RangeError is thrown.

Next: getInt8()

Go down to the smallest size: read a single byte as a signed number.

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