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.
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
Parameter
Description
byteOffset
Where 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.
littleEndian
Optional. 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 Value
Description
Number
A 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
Every BMP file starts with "BM". The letters B (0x42) and M (0x4d) are the first two bytes of a Windows bitmap file.
A little-endian check. BMP is a little-endian format, so readers use getUint16(0, true) and compare the result with 0x4d42.
The other order, and as text. Without true the result is 16973; String.fromCharCode() shows the same two bytes as the text "BM".
Compatibility
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 ChromeModern versions · Desktop & Mobile
Full support
Mozilla FirefoxModern versions · Desktop & Mobile
Full support
Apple SafariModern versions · macOS & iOS
Full support
Microsoft EdgeModern versions · Chromium
Full support
OperaModern versions
Full support
Samsung InternetModern versions · Android
Full support
Internet ExplorerRetired browser · not tested here
Legacy
Node.jsCurrent releases · checked on Node 24
Supported
DenoCurrent releases
Supported
BunCurrent 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.