JavaScript DataView setInt8() Method

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

Definition

setInt8() writes one byte into a DataView, treating your number as a signed 8-bit integer (a whole number from -128 to 127). You pass the byte offset first and the value second. Values that do not fit are not rejected: the fraction is dropped and the number wraps around, so 200 is stored as -56. The method returns undefined; you see the change when you read the byte back.

Remember
const view = new DataView(new ArrayBuffer(1));
view.setInt8(0, -100);   // undefined
view.getInt8(0);         // -100
view.getUint8(0);        // 156
view.setInt8(0, 130);
view.getInt8(0);         // -126

Syntax

Call setInt8() on a DataView object with a byte position and the value to write.

JavaScript
dataview.setInt8(byteOffset, value)
CallWhat it does
view.setInt8(byteOffset, value)Converts value to a signed 8-bit integer (-128 to 127) and writes it into the byte at byteOffset. Returns undefined.
view.setInt8(i, view.getInt8(i) - 1)Subtracts 1 from a signed byte in place; -128 minus 1 wraps around to 127.

Part of the original DataView from ES2015, so every modern browser and runtime has it. With one byte there is no byte order to choose, so setInt8() has no littleEndian argument.

Library declarations
JavaScript
DataView.prototype.setInt8(byteOffset, value)

// returns undefined; writes 1 byte starting at byteOffset
// value is converted to a signed 8-bit integer
// needs byteOffset < view.byteLength, else RangeError

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. It must be smaller 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 256 into -128 to 127. NaN and Infinity are stored as 0.

Return Value

Return ValueDescription
undefinedsetInt8() gives nothing back. Its result is the 1 byte it changed inside the buffer; read them back with getInt8().

JavaScript Examples

Example 1 — Save Positive and Negative Score Changes

A game saves each round’s score change in one signed byte. Write four changes and look at the raw bytes.

JavaScript
const changes = [3, -2, -7, 0];
const view = new DataView(new ArrayBuffer(changes.length));

changes.forEach(function (change, i) {
  view.setInt8(i, change);
});

const hex = Array.from(new Uint8Array(view.buffer), function (b) {
  return b.toString(16).padStart(2, "0");
}).join(" ");
console.log(hex);              // 03 fe f9 00
console.log(view.getInt8(2));  // -7
Try It Yourself

How It Works

  1. One byte per change. The buffer has exactly as many bytes as there are score changes, and setInt8(i, change) fills byte i.
  2. Negative numbers become high bytes. In the hex dump -2 shows as fe (254) and -7 as f9 (249): that is how two’s complement stores negatives.
  3. Reading restores the sign. getInt8(2) turns f9 back into -7, because it reads the byte as signed too.

Example 2 — What setInt8() Does With Unusual Values

Write decimals, too-large numbers and NaN, and read back what was actually stored.

JavaScript
const view = new DataView(new ArrayBuffer(1));

function store(value) {
  view.setInt8(0, value);
  return view.getInt8(0);
}

console.log(store(127.9));   // 127
console.log(store(-3.7));    // -3
console.log(store(128));     // -128
console.log(store(200));     // -56
console.log(store(NaN));     // 0
Try It Yourself

How It Works

  1. Fractions are cut, not rounded. 127.9 becomes 127 and -3.7 becomes -3: the part after the decimal point is simply dropped (toward zero).
  2. Too-big values wrap. 128 is one more than the maximum, so it wraps to -128; 200 wraps to 200 - 256 = -56.
  3. Not-a-number becomes zero. NaN has no whole-number value, so setInt8() stores 0.

Example 3 — Write Joystick Axes After a Header Byte

A 3-byte packet starts with a header byte. A second DataView that begins at byte 1 writes the signed x and y axes.

JavaScript
const packet = new ArrayBuffer(3);
new DataView(packet).setUint8(0, 0xa1);      // header byte: "joystick"

const axes = new DataView(packet, 1);        // x and y come after the header
axes.setInt8(0, -100);                       // x: pushed left
axes.setInt8(1, 45);                         // y: pushed a little up

const bytes = new Uint8Array(packet);
console.log(bytes[0] + " " + bytes[1] + " " + bytes[2]);         // 161 156 45
console.log("x=" + axes.getInt8(0) + " y=" + axes.getInt8(1));   // x=-100 y=45
Try It Yourself

How It Works

  1. A header byte first. Byte 0 holds 0xa1 (161), a made-up code that says “this packet is joystick data”.
  2. A second view for the axes. new DataView(packet, 1) starts at buffer byte 1, so axes.setInt8(0, ...) writes buffer byte 1, not byte 0.
  3. Signed axis values. A stick can lean left or right, so negative numbers make sense: -100 is stored as the byte 156 and read back as -100.

Browser & Runtime Support

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

✓ ES2015 · Baseline

DataView.prototype.setInt8()

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
setInt8() 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. The method only changes one byte in the buffer; read it back with getInt8() to see the stored value.
The value wraps around: 200 minus 256 is -56, so getInt8() later returns -56 and getUint8() returns 200.
No, it cuts them off toward zero. 127.9 is stored as 127 and -3.7 as -3.
Only because of the offset. A negative offset, or one that is not smaller than the view byteLength, throws a RangeError. Values never throw; they are converted.
For raw bytes such as file data or colours, setUint8() is clearer because you think in 0 to 255. Use setInt8() when the byte really means a small signed number, such as a change of -5.

Next: setUint16()

Write a 2-byte length field or port number, in big- or little-endian order.

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