JavaScript DataView setUint8() Method

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

Definition

setUint8() writes one byte into a DataView. The value is stored as an unsigned 8-bit integer, a whole number from 0 to 255, at the byte offset you pass first. It is the basic “put this byte here” tool for building binary data by hand, such as file signatures, colour values or protocol headers. Values outside 0 to 255 wrap around (256 becomes 0, -1 becomes 255), and the method returns undefined.

Remember
const view = new DataView(new ArrayBuffer(2));
view.setUint8(0, 0x41);   // undefined
view.getUint8(0);         // 65
view.setUint8(1, 256);
view.getUint8(1);         // 0
view.setUint8(2, 1);      // RangeError

Syntax

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

JavaScript
dataview.setUint8(byteOffset, value)
CallWhat it does
view.setUint8(byteOffset, value)Converts value to a unsigned 8-bit integer (0 to 255) and writes it into the byte at byteOffset. Returns undefined.
view.setUint8(i, 0)Clears byte i, for example inside a loop that resets a buffer to zeros.

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

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

// returns undefined; writes 1 byte starting at byteOffset
// value is converted to a unsigned 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 0 to 255. NaN and Infinity are stored as 0.

Return Value

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

JavaScript Examples

Example 1 — Write an IPv4 Address Byte by Byte

An IPv4 address has four parts from 0 to 255, so each part fits in one byte. Write them, then read the 4 bytes as one number.

JavaScript
const parts = [192, 168, 1, 10];
const view = new DataView(new ArrayBuffer(4));

parts.forEach(function (part, i) {
  view.setUint8(i, part);
});

console.log(view.getUint32(0));   // 3232235786
console.log(view.getUint8(0) + "." + view.getUint8(1) + "." + view.getUint8(2) + "." + view.getUint8(3));   // 192.168.1.10
Try It Yourself

How It Works

  1. Four parts, four bytes. An address such as 192.168.1.10 is exactly 4 bytes, so each part goes into its own byte with setUint8().
  2. The same bytes as one number. getUint32(0) reads the 4 bytes big-endian, which is the network byte order, and gets 3232235786.
  3. Back to dotted form. Reading the bytes one by one with getUint8() and joining them with dots gives the familiar address again.

Example 2 — Values Outside 0 to 255

Write a few numbers that do not fit in a byte and see what setUint8() keeps.

JavaScript
const view = new DataView(new ArrayBuffer(1));
const tests = [255, 256, 300, -1, 12.99, "7"];

tests.forEach(function (value) {
  view.setUint8(0, value);
  console.log(String(value) + " -> " + view.getUint8(0));
});
Try It Yourself

How It Works

  1. Inside the range nothing changes. 255 is the largest byte value, so it is stored as it is.
  2. Outside the range it rolls over. 256 becomes 0, 300 becomes 44 and -1 becomes 255: the value is taken modulo 256, like a car odometer rolling over.
  3. Conversions come first. 12.99 loses its fraction and becomes 12; the string "7" is converted to the number 7 before it is stored.

Example 3 — Write the First Bytes of a PNG File

Every PNG image starts with the same bytes. Write the first four with setUint8() and check them as a number and as text.

JavaScript
const view = new DataView(new ArrayBuffer(4));
view.setUint8(0, 0x89);
view.setUint8(1, 0x50);   // "P"
view.setUint8(2, 0x4e);   // "N"
view.setUint8(3, 0x47);   // "G"

console.log(view.getUint32(0).toString(16));                                 // 89504e47
console.log(new TextDecoder().decode(new Uint8Array(view.buffer, 1, 3)));   // PNG
Try It Yourself

How It Works

  1. A file signature byte by byte. A PNG file begins with the byte 0x89 followed by the letters P, N and G (0x50, 0x4e, 0x47).
  2. Check it as one number. getUint32(0) reads the 4 bytes together; in hex that is 89504e47, the value programs compare against.
  3. Check it as text. TextDecoder turns bytes 1 to 3 back into the string "PNG".

Browser & Runtime Support

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

✓ ES2015 · Baseline

DataView.prototype.setUint8()

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
setUint8() 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 writes one byte at the given offset. The value is converted to a whole number from 0 to 255 first, and the method returns undefined.
A byte only has room for 256 values. setUint8() keeps the remainder after dividing by 256, so 256 becomes 0 and 257 becomes 1.
No, it wraps. If you need clamping, use Math.min and Math.max before writing, or write through a Uint8ClampedArray.
Both write the same 8 bits. They only differ in how you think about the value: setInt8(0, -1) and setUint8(0, 255) store exactly the same byte.
Not directly. A string like "7" is converted to the number 7. To store text, turn it into bytes with TextEncoder and write each byte.

Back to the DataView Method Index

You have seen all twenty-two methods. Go back to the index to compare types, sizes and ranges.

DataView methods →

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