JavaScript DataView setInt16() Method

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

Definition

setInt16() writes a signed 16-bit whole number (-32768 to 32767) into two bytes of a DataView. You pass the byte offset, the value, and optionally true for little-endian order. Big-endian, the default, writes the high byte first; little-endian writes the low byte first. Numbers outside the range wrap around, so 40000 is stored as -25536.

Remember
const view = new DataView(new ArrayBuffer(2));
view.setInt16(0, -2);           // undefined
view.getUint8(0);               // 255
view.getUint8(1);               // 254
view.setInt16(0, -2, true);
view.getUint8(0);               // 254

Syntax

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

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

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

// returns undefined; writes 2 bytes starting at byteOffset
// value is converted to a signed 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 -32768 to 32767. 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
undefinedsetInt16() gives nothing back. Its result is the 2 bytes it changed inside the buffer; read them back with getInt16().

JavaScript Examples

Example 1 — Write Audio Samples in Little-Endian Order

Write four 16-bit audio samples the way a WAV file stores them, then look at the bytes.

JavaScript
const samples = [0, 16384, -16384, 32767];
const view = new DataView(new ArrayBuffer(samples.length * 2));

samples.forEach(function (s, i) {
  view.setInt16(i * 2, s, true);
});

const hex = Array.from(new Uint8Array(view.buffer), function (b) {
  return b.toString(16).padStart(2, "0");
}).join(" ");
console.log(hex);   // 00 00 00 40 00 c0 ff 7f
Try It Yourself

How It Works

  1. Room for every sample. Each 16-bit sample needs 2 bytes, so four samples need an 8-byte buffer, and sample i starts at byte i × 2.
  2. Little-endian like a WAV file. With true, the low byte goes first: 16384 (0x4000) is written as 00 40.
  3. Negative samples in the dump. -16384 is 0xc000 in two’s complement; little-endian order shows it as 00 c0.

Example 2 — How setInt16() Wraps Out-of-Range Numbers

Write numbers just outside the signed 16-bit range and read back what was stored.

JavaScript
const view = new DataView(new ArrayBuffer(2));

[32767.9, 32768, 40000, -40000, -32769].forEach(function (v) {
  view.setInt16(0, v);
  console.log(v + " -> " + view.getInt16(0));
});
Try It Yourself

How It Works

  1. The fraction goes first. 32767.9 is cut to 32767, the largest value that fits, so it survives unchanged.
  2. One past the top wraps to the bottom. 32768 becomes -32768, and 40000 becomes 40000 - 65536 = -25536.
  3. It wraps the other way too. -40000 becomes 25536 and -32769 becomes 32767: 65536 is added until the number fits.

Example 3 — Store Elevations, Including Negative Ones

Save two elevations in metres as signed 16-bit numbers, then read one back with the wrong type to see why the type matters.

JavaScript
const places = [["Dead Sea shore", -430], ["Mount Everest", 8849]];
const view = new DataView(new ArrayBuffer(4));

places.forEach(function (place, i) {
  view.setInt16(i * 2, place[1]);
});

console.log(view.getInt16(0) + " m, " + view.getInt16(2) + " m");   // -430 m, 8849 m
console.log("read as unsigned: " + view.getUint16(0));              // read as unsigned: 65106
Try It Yourself

How It Works

  1. Heights can be negative. Some places lie below sea level, so a signed type is needed; 16 bits easily cover -430 to 8849 metres.
  2. Two values, four bytes. Each height uses 2 bytes in the default big-endian order, at offsets 0 and 2.
  3. Wrong type, wrong answer. Reading the first height with getUint16() ignores the sign and shows 65106, which is why the reader must know the type.

Browser & Runtime Support

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

✓ ES2015 · Baseline

DataView.prototype.setInt16()

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

undefined. It writes two bytes into the buffer and has no other result.
It wraps around by 65536. For example setInt16(0, 40000) stores -25536.
Pass true when the format you are writing is little-endian, such as WAV audio. Leave it out for big-endian formats such as most network protocols.
Exactly two: the byte at the offset and the one after it. The offset plus 2 must not go past the end of the view.
No. The fraction is cut off toward zero, so 1.5 becomes 1 and -1.5 becomes -1.

Next: setInt32()

Write a signed 4-byte number, and see how setInt32() handles decimals and huge values.

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