JavaScript DataView getUint8() Method

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

Definition

getUint8() reads one byte from a DataView and returns it as an unsigned whole number from 0 to 255. “Unsigned” means the number can never be negative: all 8 bits count toward the value. This is the most natural way to look at raw data, because a byte is usually described as a value from 0 to 255 (or 00 to ff in hexadecimal, base 16). Pass the byte offset: the position of the byte, counted from 0 at the start of the view.

Remember
const view = new DataView(new Uint8Array([0, 128, 255]).buffer);
view.getUint8(0);                // 0
view.getUint8(2);                // 255
view.getUint8(2).toString(16);   // "ff"
view.getInt8(2);                 // -1
view.getUint8(3);                // RangeError

Syntax

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

JavaScript
dataview.getUint8(byteOffset)
CallWhat it does
view.getUint8(byteOffset)Reads the single byte at byteOffset and returns an unsigned whole number (never negative) from 0 to 255.
view.getUint8(i).toString(16)Reads byte i and shows it in hexadecimal (base 16), the usual way to look at raw bytes.

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

Library declarations
JavaScript
DataView.prototype.getUint8(byteOffset)

// returns a Number: 0 to 255
// 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 0 to 255, built from the 1 byte at byteOffset. It is never negative.

JavaScript Examples

Example 1 — Read the Colour of One Pixel

Image data often stores a pixel as four bytes: red, green, blue and alpha (opacity). Read them and build a CSS colour.

JavaScript
const pixel = new DataView(new Uint8Array([255, 128, 0, 200]).buffer);

const r = pixel.getUint8(0);
const g = pixel.getUint8(1);
const b = pixel.getUint8(2);
const a = pixel.getUint8(3);

console.log("rgb(" + r + ", " + g + ", " + b + ")");   // rgb(255, 128, 0)
console.log("alpha: " + (a / 255).toFixed(2));         // alpha: 0.78
Try It Yourself

How It Works

  1. Four bytes, one pixel. Canvas image data and many image files keep red, green, blue and alpha in that order, one byte each.
  2. Read each channel. getUint8() at offsets 0 to 3 returns 255, 128, 0 and 200: an orange colour.
  3. Turn alpha into a fraction. Dividing 200 by 255 gives about 0.78, the opacity value CSS expects in rgba().

Example 2 — Check Yes/No Flags Packed Into One Byte

One status byte can hold eight yes/no switches. Read it with getUint8() and test single bits with the bitwise AND operator.

JavaScript
const FLAG_ONLINE = 1;     // bit 0
const FLAG_ADMIN = 2;      // bit 1
const FLAG_MUTED = 4;      // bit 2

const view = new DataView(new Uint8Array([0b00000101]).buffer);
const flags = view.getUint8(0);

console.log(flags);                                        // 5
console.log("online: " + ((flags & FLAG_ONLINE) !== 0));   // online: true
console.log("admin: " + ((flags & FLAG_ADMIN) !== 0));     // admin: false
console.log("muted: " + ((flags & FLAG_MUTED) !== 0));     // muted: true
Try It Yourself

How It Works

  1. Eight switches in one byte. Each bit of a status byte can be a yes/no flag. Here bit 0 means online, bit 1 admin and bit 2 muted.
  2. Read the byte as a plain number. getUint8(0) returns 5, which is 101 in binary: bits 0 and 2 are on.
  3. Test one bit with AND. The bitwise & keeps only the bit you ask about, so a result other than 0 means that flag is set.

Example 3 — Print Every Byte in Hexadecimal

Turn a short text into bytes, then loop over them with getUint8() to print a hex dump, the classic way to inspect binary data.

JavaScript
const bytes = new TextEncoder().encode("Hi!");
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
const parts = [];

for (let i = 0; i < view.byteLength; i++) {
  parts.push(view.getUint8(i).toString(16).padStart(2, "0"));
}
console.log(parts.join(" "));   // 48 69 21

try {
  view.getUint8(view.byteLength);
} catch (e) {
  console.log(e.name);          // RangeError
}
Try It Yourself

How It Works

  1. Text becomes bytes. TextEncoder turns "Hi!" into its UTF-8 bytes. Passing bytes.byteOffset and bytes.byteLength gives the view exactly the same window.
  2. Hex for every byte. The loop reads each byte with getUint8(), converts it to base 16 and pads it to two digits: H is 48, i is 69 and ! is 21.
  3. Stop before byteLength. The last valid offset is byteLength - 1. Asking for offset byteLength throws a RangeError.

Browser & Runtime Support

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

✓ ES2015 · Baseline

DataView.prototype.getUint8()

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
getUint8() 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 0 to 255: the value of the byte at the offset you pass, read without a sign.
Call toString(16) on the number and pad it to two characters: view.getUint8(0).toString(16).padStart(2, "0") gives "0a" for the value 10.
For a valid index, yes. The differences are that getUint8() counts from the view start, works on a DataView that mixes types, and throws a RangeError past the end instead of returning undefined.
It reads a single byte, and one byte has no order to swap. Extra arguments are simply ignored.
No. All 8 bits count as value, so the smallest result is 0. Use getInt8() if the byte stores a signed number.

Next: setBigInt64()

Now switch to writing: store an exact 64-bit whole number, such as a timestamp in milliseconds.

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