JavaScript DataView getInt8() Method

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

Definition

getInt8() reads one byte from a DataView and returns it as a signed whole number from -128 to 127. A byte is 8 bits, the smallest unit of memory, and it can hold 256 different patterns. “Signed” means half of those patterns stand for negative numbers: the bytes 0 to 127 read as themselves, and the bytes 128 to 255 read as -128 to -1. You choose the byte with a byte offset: its position, counted from 0 at the start of the view.

Remember
const view = new DataView(new ArrayBuffer(2));
view.setUint8(0, 200);
view.getInt8(0);      // -56
view.getUint8(0);     // 200
view.getInt8(1);      // 0
view.getInt8(2);      // RangeError

Syntax

Call getInt8() on a DataView object with the position of the byte you want.

JavaScript
dataview.getInt8(byteOffset)
CallWhat it does
view.getInt8(byteOffset)Reads the single byte at byteOffset and returns a signed whole number from -128 to 127.
view.getInt8(view.byteLength - 1)Reads the last byte of the view as a signed number.

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 getInt8() has no littleEndian argument.

Library declarations
JavaScript
DataView.prototype.getInt8(byteOffset)

// returns a Number: -128 to 127
// reads 1 byte starting at byteOffset (counted from the start of the view)
// needs byteOffset < view.byteLength, else RangeError

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. It must be smaller than view.byteLength. A negative or too-large offset throws a RangeError.

Return Value

Return ValueDescription
NumberA whole Number from -128 to 127, built from the 1 byte at byteOffset using two’s complement for negative values.

JavaScript Examples

Example 1 — Read Temperature Changes Stored as Signed Bytes

A weather station sends each temperature change as one signed byte. Read all four changes, including two negative ones.

JavaScript
const bytes = new Uint8Array([5, 251, 128, 127]);
const view = new DataView(bytes.buffer);

for (let i = 0; i < view.byteLength; i++) {
  console.log("change " + i + ": " + view.getInt8(i) + " degrees");
}
Try It Yourself

How It Works

  1. Raw bytes go in. The Uint8Array holds the plain byte values 5, 251, 128 and 127, and bytes.buffer is the ArrayBuffer behind it.
  2. Each byte is read as signed. Bytes up to 127 keep their value. 251 is above 127, so getInt8() subtracts 256 and returns -5.
  3. The two extremes. The byte 128 is the most negative value, -128, and 127 is the largest positive one.

Example 2 — The Same Byte as Signed and Unsigned

Store the byte 255 and read it with getUint8() and getInt8(). The bits are identical; only the meaning changes.

JavaScript
const view = new DataView(new ArrayBuffer(1));
view.setUint8(0, 255);

console.log(view.getUint8(0));   // 255
console.log(view.getInt8(0));    // -1
console.log(view.getInt8(0) === view.getUint8(0) - 256);   // true
Try It Yourself

How It Works

  1. One byte with every bit set. setUint8(0, 255) stores the bit pattern 11111111 in the only byte of the view.
  2. Unsigned reading. getUint8() treats all 8 bits as a plain number, so it gives 255.
  3. Signed reading. getInt8() uses two’s complement: when the top bit is 1, the value is the unsigned number minus 256, so 255 becomes -1.

Example 3 — A View That Starts in the Middle of a Buffer

Create a DataView that skips the first two bytes of a buffer. Its offset 0 is buffer byte 2, and it ends where the buffer ends.

JavaScript
const buffer = new Uint8Array([1, 2, 254, 253, 252]).buffer;
const tail = new DataView(buffer, 2);    // starts at buffer byte 2

console.log(tail.byteLength);    // 3
console.log(tail.getInt8(0));    // -2
console.log(tail.getInt8(2));    // -4

try {
  tail.getInt8(3);
} catch (e) {
  console.log(e.name);           // RangeError
}
Try It Yourself

How It Works

  1. The view starts later. new DataView(buffer, 2) skips the first two bytes, so the view covers only the last 3 bytes of the buffer.
  2. Offsets count from the view start. getInt8(0) reads buffer byte 2 (254, shown as -2), and getInt8(2) reads the last buffer byte (252, shown as -4).
  3. One step too far. Offset 3 is outside the 3-byte view, so getInt8() throws a RangeError even though the buffer itself has 5 bytes.

Browser & Runtime Support

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

✓ ES2015 · Baseline

DataView.prototype.getInt8()

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

A whole number from -128 to 127, made from the single byte at the offset you pass. Bytes 128 to 255 come back as negative numbers.
Both read one byte. getUint8() returns 0 to 255, while getInt8() treats the top bit as a minus sign and returns -128 to 127. The byte 200 is 200 for getUint8() and -56 for getInt8().
Byte order only matters when a number is spread over two or more bytes. getInt8() reads exactly one byte, so there is nothing to reorder.
It throws a RangeError. The offset must be at least 0 and smaller than the byteLength of the view.
It gives the same number, but a DataView can mix types in one buffer: you can call getInt8() at offset 0 and getFloat32() at offset 1 on the same view.

Next: getUint16()

Read two bytes as a number from 0 to 65535, such as a network port.

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