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.
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
Parameter
Description
byteOffset
Where 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.
value
The 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.
littleEndian
Optional. 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 Value
Description
undefined
setInt32() 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.