setFloat16() writes a number into two bytes of a DataView as a 16-bit floating point value (half precision). It rounds your number to the nearest value that 16 bits can hold, about 3 significant digits and at most 65504, so it is best for data where small errors do not matter, such as colours, textures or machine-learning weights. It is new in ES2025: check typeof DataView.prototype.setFloat16 === "function" before you use it.
Converts value to a 16-bit floating point number (half precision) and writes it into the 2 bytes starting at byteOffset, big-endian. Returns undefined.
view.setFloat16(byteOffset, value, true)
Writes the same 2 bytes in little-endian order, so the least significant byte lands at byteOffset.
One of the newest DataView methods (ES2025, together with Float16Array and Math.f16round()). Node.js 24 has it; older browsers do not, so feature-detect it with typeof DataView.prototype.setFloat16 === "function". Unlike a Float16Array, a DataView has no alignment rule: setFloat16() works at any byte position, even one that is not a multiple of 2.
Library declarations
JavaScript
DataView.prototype.setFloat16(byteOffset, value)
DataView.prototype.setFloat16(byteOffset, value, littleEndian)
// returns undefined; writes 2 bytes starting at byteOffset
// value is converted to a 16-bit floating point number
// needs byteOffset + 2 <= view.byteLength, else RangeError
// littleEndian: false or left out = big-endian
Parameters
Parameter
Description
byteOffset
Where 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.
value
The number to store. It is rounded to the nearest 16-bit floating point number, so 0.1 comes back as 0.0999755859375. Values of 65520 or more (or -65520 or less) become Infinity or -Infinity.
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
undefined
setFloat16() gives nothing back. Its result is the 2 bytes it changed inside the buffer; read them back with getFloat16().
JavaScript Examples
Example 1 — Store Four Weights in Eight Bytes
Write four decimal numbers as half floats, compare the memory use, and read them back.
JavaScript
const weights = [0.5, -1.25, 2, 0.1];
const half = new DataView(new ArrayBuffer(weights.length * 2));
weights.forEach(function (w, i) {
half.setFloat16(i * 2, w);
});
console.log("bytes used: " + half.byteLength + " instead of " + weights.length * 4); // bytes used: 8 instead of 16
for (let i = 0; i < weights.length; i++) {
console.log(half.getFloat16(i * 2));
}
bytes used: 8 instead of 16
0.5
-1.25
2
0.0999755859375
The Try It page first checks typeof DataView.prototype.getFloat16 === "function" (and the same for setFloat16). If your browser does not have them yet, it says so and shows the output above.
How It Works
Two bytes per number. Four weights fit into 8 bytes; as 32-bit floats they would need 16, and as normal Numbers 32.
Simple values survive exactly. 0.5, -1.25 and 2 are made of powers of two, so setFloat16() stores them without any error.
Others are rounded. 0.1 has no exact binary form; in 16 bits it is stored as 0.0999755859375.
Example 2 — The Edges of the Half-Float Range
Write values near the top of the range and one tiny value, then read what was stored.
The Try It page first checks typeof DataView.prototype.getFloat16 === "function" (and the same for setFloat16). If your browser does not have them yet, it says so and shows the output above.
How It Works
The top of the range. 65504 is the largest finite half float, and 65519 is still close enough to round down to it.
Past the edge is Infinity. From 65520 on, the nearest value is Infinity, and -70000 becomes -Infinity. No error is thrown.
Very small values vanish. 1e-8 is below the smallest half float (about 6e-8), so it is stored as 0.
Example 3 — Look at the Bytes setFloat16() Writes
Write 1 in both byte orders and -2 big-endian, then print the six bytes.
The Try It page first checks typeof DataView.prototype.getFloat16 === "function" (and the same for setFloat16). If your browser does not have them yet, it says so and shows the output above.
How It Works
The bit pattern of 1. As a half float, 1 is 0x3c00, so the big-endian write at offset 0 gives 3c 00.
Little-endian reverses it. With true the low byte comes first, so offset 2 holds 00 3c.
The sign is the top bit. -2 is 0xc000: the first bit of c0 is the minus sign.
Compatibility
Browser & Runtime Support
setFloat16() is one of the newest JavaScript features (ES2025, the Float16 proposal). Engines added it only recently.
✓ ES2025 · feature-detect
DataView.prototype.setFloat16()
Checked in Node.js 24. In browsers it is limited to the newest versions, so test for it with typeof before calling it.
NewSupport is still growing
Google ChromeNewest versions only
Check version
Mozilla FirefoxNewest versions only
Check version
Apple SafariNewest versions only
Check version
Microsoft EdgeNewest versions only
Check version
OperaNewest versions only
Check version
Samsung InternetNewest versions only
Check version
Internet ExplorerNo Float16 support
Not supported
Node.jsAvailable in Node 24 (checked)
Supported
DenoRecent releases
Check version
BunRecent releases
Check version
setFloat16()Newest engines only
Bottom line: Guard it: if (typeof DataView.prototype.setFloat16 === "function") { ... }. Without it, store the value with setFloat32() instead, which costs 4 bytes instead of 2.
Frequently Asked Questions
It rounds a number to the nearest 16-bit floating point value and writes those two bytes at the given offset. It returns undefined.
Only about 3 significant digits fit into 16 bits, and 0.1 has no exact binary form, so the nearest storable value is used.
Values from 65520 up become Infinity (and from -65520 down become -Infinity). Values between 65504 and 65520 round down to 65504.
Check typeof DataView.prototype.setFloat16 === "function". If it is missing, store the number with setFloat32() instead, which uses 4 bytes.
Yes. Math.f16round(x) returns the value setFloat16() would store, in engines that support the Float16 additions.