JavaScript DataView setUint16() Method

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

Definition

setUint16() writes an unsigned 16-bit whole number (0 to 65535) into two bytes of a DataView. Give it the byte offset, the value and, if needed, true for little-endian order. It is what you reach for when you build a binary message with a 2-byte length, port or ID field. Values outside the range wrap around modulo 65536.

Remember
const view = new DataView(new ArrayBuffer(2));
view.setUint16(0, 0x1234);    // undefined
view.getUint8(0);             // 18
view.setUint16(0, 0x1234, true);
view.getUint8(0);             // 52
view.getUint16(0, true);      // 4660

Syntax

Call setUint16() on a DataView object with a byte position and the value to write, plus true if you need little-endian order.

JavaScript
dataview.setUint16(byteOffset, value)
dataview.setUint16(byteOffset, value, littleEndian)
CallWhat it does
view.setUint16(byteOffset, value)Converts value to a unsigned 16-bit integer (0 to 65535) and writes it into the 2 bytes starting at byteOffset, big-endian. Returns undefined.
view.setUint16(byteOffset, value, true)Writes the same 2 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 Uint16Array, a DataView has no alignment rule: setUint16() works at any byte position, even one that is not a multiple of 2.

Library declarations
JavaScript
DataView.prototype.setUint16(byteOffset, value)
DataView.prototype.setUint16(byteOffset, value, littleEndian)

// returns undefined; writes 2 bytes starting at byteOffset
// value is converted to a unsigned 16-bit integer
// needs byteOffset + 2 <= view.byteLength, else RangeError
// littleEndian: false or left out = big-endian

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. All 2 bytes must fit: byteOffset + 2 may not be larger 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 65536 into 0 to 65535. NaN and Infinity are stored as 0.
littleEndianOptional. Leave it out (or pass false) for big-endian: the most significant of the 2 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 ValueDescription
undefinedsetUint16() gives nothing back. Its result is the 2 bytes it changed inside the buffer; read them back with getUint16().

JavaScript Examples

Example 1 — Build a Packet With a 2-Byte Length Field

Put the length of a short text in the first two bytes of a packet, followed by the text itself.

JavaScript
const payload = new TextEncoder().encode("Hi!");
const packet = new Uint8Array(2 + payload.length);
const view = new DataView(packet.buffer);

view.setUint16(0, payload.length);   // length first
packet.set(payload, 2);              // then the text

const hex = Array.from(packet, function (b) { return b.toString(16).padStart(2, "0"); }).join(" ");
console.log(hex);   // 00 03 48 69 21
Try It Yourself

How It Works

  1. Text to bytes. TextEncoder turns "Hi!" into 3 UTF-8 bytes: 48 69 21.
  2. Reserve two bytes for the length. The packet is 2 + 3 bytes long, and setUint16(0, 3) fills the first two with 00 03 (big-endian).
  3. Copy the text after it. packet.set(payload, 2) places the text bytes right after the length field.

Example 2 — Write Port 8080 Big-Endian and Little-Endian

Write the same number twice, once in each byte order, and compare the bytes.

JavaScript
const view = new DataView(new ArrayBuffer(4));
view.setUint16(0, 8080);          // big-endian
view.setUint16(2, 8080, true);    // little-endian

const bytes = new Uint8Array(view.buffer);
console.log(bytes[0].toString(16) + " " + bytes[1].toString(16));   // 1f 90
console.log(bytes[2].toString(16) + " " + bytes[3].toString(16));   // 90 1f
Try It Yourself

How It Works

  1. Same number twice. Port 8080 is written at offset 0 in big-endian order and at offset 2 in little-endian order.
  2. Big-endian: high byte first. 8080 is 0x1f90, so bytes 0 and 1 hold 1f, then 90.
  3. Little-endian: low byte first. Bytes 2 and 3 hold 90, then 1f: the reverse order.

Example 3 — A 16-bit Counter Going Past 65535

Add 1 to a counter that is already at the maximum, and try a few other values outside the range.

JavaScript
const view = new DataView(new ArrayBuffer(2));
view.setUint16(0, 65535);

view.setUint16(0, view.getUint16(0) + 1);
console.log(view.getUint16(0));   // 0

view.setUint16(0, 65537);
console.log(view.getUint16(0));   // 1

view.setUint16(0, -1);
console.log(view.getUint16(0));   // 65535
Try It Yourself

How It Works

  1. Start at the maximum. 65535 is the biggest value two unsigned bytes can hold.
  2. Adding one rolls over. 65535 + 1 is 65536, which does not fit, so setUint16() stores 0, like a counter going back to zero.
  3. Wrap-around in both directions. 65537 is stored as 1, and -1 is stored as 65535.

Browser & Runtime Support

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

✓ ES2015 · Baseline

DataView.prototype.setUint16()

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
setUint16() 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 converts a number to a 16-bit unsigned value from 0 to 65535 and writes it into two bytes at the given offset.
Pass true as the third argument: view.setUint16(0, 8080, true) writes the bytes 90 1f instead of 1f 90.
It stores 0. Values are taken modulo 65536, so they wrap around instead of causing an error.
Yes. A DataView has no alignment rule, so offsets such as 1 or 3 are fine as long as two bytes fit.
Nothing useful: the return value is undefined. Check the stored value with getUint16() using the same byte order.

Next: setUint32()

Write 4 bytes at once: image sizes, IPv4 addresses and the wrap-around at 4294967296.

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