JavaScript DataView getUint16() Method

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

Definition

getUint16() reads two bytes from a DataView and returns an unsigned 16-bit whole number from 0 to 65535. It is the usual choice for counts, lengths, port numbers and IDs that never go below zero. As with every multi-byte method, the byte order matters: big-endian is the default (also called network byte order), and little-endian is used when you pass true.

Remember
const view = new DataView(new Uint8Array([0x1f, 0x90]).buffer);
view.getUint16(0);         // 8080
view.getUint16(0, true);   // 36895
view.getInt16(0, true);    // -28641
view.getUint16(1);         // RangeError

Syntax

Call getUint16() on a DataView object with a byte position and, if the data is little-endian, true.

JavaScript
dataview.getUint16(byteOffset)
dataview.getUint16(byteOffset, littleEndian)
CallWhat it does
view.getUint16(byteOffset)Reads the 2 bytes starting at byteOffset in big-endian order (most significant byte first) and returns an unsigned whole number (never negative) from 0 to 65535.
view.getUint16(byteOffset, true)Reads the same 2 bytes in little-endian order (least significant byte first), the order used by most file formats from PCs, such as WAV and BMP.

Part of the original DataView from ES2015, so every modern browser and runtime has it. Unlike a Uint16Array, a DataView has no alignment rule: getUint16() works at any byte position, even one that is not a multiple of 2.

Library declarations
JavaScript
DataView.prototype.getUint16(byteOffset)
DataView.prototype.getUint16(byteOffset, littleEndian)

// returns a Number: 0 to 65535
// reads 2 bytes starting at byteOffset (counted from the start of the view)
// needs byteOffset + 2 <= view.byteLength, else RangeError
// littleEndian: false or left out = big-endian

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. All 2 bytes must fit: byteOffset + 2 may not be larger than view.byteLength. A negative or too-large offset throws a RangeError.
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
NumberA whole Number from 0 to 65535, built from the 2 bytes at byteOffset. It is never negative.

JavaScript Examples

Example 1 — Read the Ports From a TCP Header

The first four bytes of a TCP header are the source port and the destination port, each 16 bits, big-endian.

JavaScript
const header = new DataView(new Uint8Array([0x01, 0xbb, 0xc8, 0x22]).buffer);

const sourcePort = header.getUint16(0);
const destPort = header.getUint16(2);

console.log("from port " + sourcePort);   // from port 443
console.log("to port " + destPort);       // to port 51234
Try It Yourself

How It Works

  1. The start of a TCP header. A TCP packet begins with two 16-bit port numbers, written big-endian: bytes 01 bb, then c8 22.
  2. No argument needed for network order. getUint16(0) uses big-endian by default, which is exactly network byte order, and returns 443 (HTTPS).
  3. Next field, next two bytes. The destination port starts at offset 2 and reads as 51234, a typical temporary port on the client side.

Example 2 — Read a Message That Starts With Its Length

Many protocols send the size of a message first. Read the 2-byte length, then exactly that many bytes of data.

JavaScript
const bytes = new Uint8Array([0x00, 0x03, 0x0a, 0x0b, 0x0c, 0xff]);
const view = new DataView(bytes.buffer);

const length = view.getUint16(0);
const payload = bytes.slice(2, 2 + length);

console.log("length: " + length);                          // length: 3
console.log("payload: " + Array.from(payload).join(", "));   // payload: 10, 11, 12
Try It Yourself

How It Works

  1. A length prefix. The first 2 bytes, 00 03, say that 3 bytes of data follow.
  2. Read the size, then the data. getUint16(0) returns 3, and bytes.slice(2, 5) copies exactly those 3 bytes.
  3. Extra bytes are left alone. The final ff belongs to the next message, so using the length keeps the reader from going too far.

Example 3 — Recognise a Bitmap File by Its First Two Bytes

Windows bitmap files start with the letters "BM". Read them as one little-endian 16-bit number, as BMP readers do.

JavaScript
const view = new DataView(new Uint8Array([0x42, 0x4d]).buffer);

console.log(view.getUint16(0, true));              // 19778
console.log(view.getUint16(0, true) === 0x4d42);   // true
console.log(view.getUint16(0));                    // 16973
console.log(String.fromCharCode(view.getUint8(0), view.getUint8(1)));   // BM
Try It Yourself

How It Works

  1. Every BMP file starts with "BM". The letters B (0x42) and M (0x4d) are the first two bytes of a Windows bitmap file.
  2. A little-endian check. BMP is a little-endian format, so readers use getUint16(0, true) and compare the result with 0x4d42.
  3. The other order, and as text. Without true the result is 16973; String.fromCharCode() shows the same two bytes as the text "BM".

Browser & Runtime Support

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

✓ ES2015 · Baseline

DataView.prototype.getUint16()

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
getUint16() 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 65535, built from the two bytes at the offset. It is never negative.
DataView uses big-endian unless told otherwise, which matches network byte order. Pass true as the second argument for little-endian data.
Ports in network headers are 16-bit big-endian values, so view.getUint16(offset) with no second argument gives the right number.
getUint16() needs two bytes. If the offset points at the last byte, it throws a RangeError instead of reading half a number.
For everyday code the difference does not matter. A Uint16Array is handy for long lists in the machine byte order; getUint16() is better for mixed fields and a fixed byte order.

Next: getUint32()

Read 4 bytes as one unsigned number: Unix timestamps, file signatures and image sizes.

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