JavaScript DataView getInt16() Method

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

Definition

getInt16() reads two bytes from a DataView and returns them as a signed 16-bit whole number from -32768 to 32767. Because the number is spread over two bytes, their order matters. This is called endianness: big-endian puts the most significant (“biggest”) byte first, little-endian puts the least significant byte first. getInt16() reads big-endian unless you pass true as the second argument.

Remember
const view = new DataView(new Uint8Array([0xff, 0x38]).buffer);
view.getInt16(0);         // -200
view.getInt16(0, true);   // 14591
view.getUint16(0);        // 65336
view.getInt16(1);         // RangeError

Syntax

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

JavaScript
dataview.getInt16(byteOffset)
dataview.getInt16(byteOffset, littleEndian)
CallWhat it does
view.getInt16(byteOffset)Reads the 2 bytes starting at byteOffset in big-endian order (most significant byte first) and returns a signed whole number from -32768 to 32767.
view.getInt16(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 Int16Array, a DataView has no alignment rule: getInt16() works at any byte position, even one that is not a multiple of 2.

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

// returns a Number: -32768 to 32767
// 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 -32768 to 32767, built from the 2 bytes at byteOffset using two’s complement for negative values.

JavaScript Examples

Example 1 — Find the Loudest Sample in 16-bit Audio

CD-quality audio stores every sample as a signed 16-bit number. Read four little-endian samples and find the loudest one.

JavaScript
const samples = new DataView(new ArrayBuffer(8));
samples.setInt16(0, 1200, true);
samples.setInt16(2, -9000, true);
samples.setInt16(4, 4500, true);
samples.setInt16(6, -300, true);

let peak = 0;
for (let i = 0; i < samples.byteLength; i += 2) {
  peak = Math.max(peak, Math.abs(samples.getInt16(i, true)));
}
console.log("peak: " + peak);                                         // peak: 9000
console.log("loudness: " + (peak / 32768 * 100).toFixed(1) + "%");   // loudness: 27.5%
Try It Yourself

How It Works

  1. Two bytes per sample. WAV files keep their 16-bit samples in little-endian order, so the setup writes them with true.
  2. Step through by two. The loop moves 2 bytes at a time and reads each sample with getInt16(i, true).
  3. Find the loudest point. Math.abs() ignores the sign, because a sound wave swings below zero too. The peak, 9000, is about 27.5% of the maximum 32768.

Example 2 — Read the Same Two Bytes in Both Orders

Read two pairs of bytes once big-endian and once little-endian, and see how the numbers change.

JavaScript
const view = new DataView(new Uint8Array([0x80, 0x00, 0x01, 0x02]).buffer);

console.log(view.getInt16(0));         // -32768
console.log(view.getInt16(0, true));   // 128
console.log(view.getInt16(2));         // 258
console.log(view.getInt16(2, true));   // 513
Try It Yourself

How It Works

  1. Big-endian by default. getInt16(0) treats 0x80 as the high byte, giving 0x8000, which is -32768 as a signed number.
  2. Little-endian swaps the roles. With true, 0x00 is the high byte and 0x80 the low byte, so the result is 0x0080 = 128.
  3. The same idea with small numbers. Bytes 01 02 read big-endian give 1 × 256 + 2 = 258; little-endian gives 2 × 256 + 1 = 513.

Example 3 — A 16-bit Value at an Odd Position

Packed formats often place a 2-byte number right after a 1-byte field. DataView reads it there; an Int16Array cannot.

JavaScript
const buffer = new Uint8Array([0x07, 0xfe, 0x0c, 0x00]).buffer;
const view = new DataView(buffer);

console.log(view.getInt16(1));   // -500

try {
  new Int16Array(buffer, 1, 1);
} catch (e) {
  console.log("Int16Array: " + e.name);   // Int16Array: RangeError
}
Try It Yourself

How It Works

  1. A number at an odd position. Byte 0 (0x07) is a 1-byte field, so the 16-bit value starts at offset 1.
  2. DataView does not care about alignment. getInt16(1) reads the bytes fe 0c and returns -500 without complaint.
  3. A typed array refuses. An Int16Array must start at an even byte offset, so creating one at offset 1 throws a RangeError. That is a key reason to use DataView for file formats.

Browser & Runtime Support

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

✓ ES2015 · Baseline

DataView.prototype.getInt16()

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

From -32768 to 32767. Two bytes hold 65536 patterns, and half of them are used for negative numbers.
It is the order of the two bytes. Big-endian (the default) puts the high byte first; little-endian, chosen with true, puts the low byte first.
WAV samples are little-endian. Call getInt16(offset, true); without true the bytes are read the wrong way round.
Yes. DataView has no alignment rule, so getInt16(1) or getInt16(3) work as long as both bytes are inside the view.
They read the same two bytes. getInt16() returns -32768 to 32767, getUint16() returns 0 to 65535, so 0xffff is -1 for one and 65535 for the other.

Next: getInt32()

Read 4 bytes as a signed number, such as money in cents or GPS coordinates.

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