JavaScript DataView getFloat16() Method

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

Definition

getFloat16() reads two bytes from a DataView and returns them as a 16-bit floating point number, also called half precision. A floating point number is a number with a decimal point that can “float”, so it can be tiny or large; with only 16 bits it keeps about 3 significant digits and reaches at most 65504. Graphics cards and machine-learning models use this format to save memory. It is one of the newest DataView methods (ES2025), so feature-detect it first.

Remember
const view = new DataView(new Uint8Array([0x3c, 0x00, 0x00, 0x3c]).buffer);
view.getFloat16(0);         // 1
view.getFloat16(2, true);   // 1
view.getFloat16(1);         // 0
view.getFloat16(3);         // RangeError

Syntax

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

JavaScript
dataview.getFloat16(byteOffset)
dataview.getFloat16(byteOffset, littleEndian)
CallWhat it does
view.getFloat16(byteOffset)Reads the 2 bytes starting at byteOffset in big-endian order (most significant byte first) and returns a 16-bit floating point number (half precision).
view.getFloat16(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.

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.getFloat16 === "function". Unlike a Float16Array, a DataView has no alignment rule: getFloat16() works at any byte position, even one that is not a multiple of 2.

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

// returns a Number: about -65504 to 65504, about 3 significant digits
// 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
NumberThe 2 bytes decoded as a 16-bit floating point number (half precision) and turned into a normal JavaScript Number (range about -65504 to 65504, about 3 significant digits). It can also be NaN, Infinity, -Infinity or -0.

JavaScript Examples

Example 1 — Read Machine-Learning Weights Stored as Half Floats

Neural networks store millions of weights, often as 2-byte floats. Put four known half-float bit patterns in a buffer and decode them.

JavaScript
const weights = new DataView(new ArrayBuffer(8));
weights.setUint16(0, 0x3c00);   // bit pattern of 1
weights.setUint16(2, 0xc000);   // -2
weights.setUint16(4, 0x3800);   // 0.5
weights.setUint16(6, 0x3555);   // about one third

for (let i = 0; i < weights.byteLength; i += 2) {
  console.log("weight " + i / 2 + ": " + weights.getFloat16(i));
}
Try It Yourself

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

  1. Half-size numbers for a model. Using 2 bytes per weight instead of 4 or 8 lets a big model fit into much less memory.
  2. Write raw bit patterns. setUint16() places known 16-bit float patterns in the buffer: 0x3c00 is 1, 0xc000 is -2 and 0x3800 is 0.5.
  3. Decode each weight. getFloat16(i) turns every 2-byte pattern into a normal JavaScript Number. The last one shows that one third can only be stored roughly.

Example 2 — Round Trip Through Two Bytes

Write a few numbers as half floats and read them back to see exactly what 16 bits can keep.

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

[0.1, 3.14159, 1000.7, 2049].forEach(function (x) {
  view.setFloat16(0, x);
  console.log(x + " -> " + view.getFloat16(0));
});
Try It Yourself

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

  1. Write, then read back. Each number goes through setFloat16() and getFloat16(), so the output shows what survives.
  2. About three digits survive. 0.1 becomes 0.0999755859375 and 3.14159 becomes 3.140625: close, but not exact.
  3. Bigger numbers get coarser. The gaps between storable values grow with size: below 1024 they are 0.5 apart and above 2048 they are 2 apart, so 1000.7 becomes 1000.5 and 2049 becomes 2048.

Example 3 — Special Half-Float Bit Patterns

Some 16-bit patterns have special meanings. Decode the largest finite value, both infinities and NaN.

JavaScript
const view = new DataView(new Uint8Array([0x7b, 0xff, 0x7c, 0x00, 0xfc, 0x00, 0x7e, 0x00]).buffer);

console.log(view.getFloat16(0));   // 65504
console.log(view.getFloat16(2));   // Infinity
console.log(view.getFloat16(4));   // -Infinity
console.log(view.getFloat16(6));   // NaN
Try It Yourself

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

  1. The largest finite value. The pattern 7b ff is the biggest normal half-precision number, 65504.
  2. Infinity has its own pattern. All exponent bits on with a zero fraction (7c 00) means Infinity; with the sign bit as well (fc 00) it is -Infinity.
  3. Not a number. Exponent bits on with a non-zero fraction (7e 00) decodes to NaN.

Browser & Runtime Support

getFloat16() is one of the newest JavaScript features (ES2025, the Float16 proposal). Engines added it only recently.

✓ ES2025 · feature-detect

DataView.prototype.getFloat16()

Checked in Node.js 24. In browsers it is limited to the newest versions, so test for it with typeof before calling it.

New Support is still growing
Google Chrome Newest versions only
Check version
Mozilla Firefox Newest versions only
Check version
Apple Safari Newest versions only
Check version
Microsoft Edge Newest versions only
Check version
Opera Newest versions only
Check version
Samsung Internet Newest versions only
Check version
Internet Explorer No Float16 support
Not supported
Node.js Available in Node 24 (checked)
Supported
Deno Recent releases
Check version
Bun Recent releases
Check version
getFloat16() Newest engines only

Bottom line: Guard it: if (typeof DataView.prototype.getFloat16 === "function") { ... }. Without it, store the value with getFloat32() instead, which costs 4 bytes instead of 2.

Frequently Asked Questions

A method that reads two bytes as a 16-bit (half precision) floating point number and returns it as a normal JavaScript Number.
Not yet. It was added in ES2025, so only the newest browser versions have it. Node.js 24 supports it. Check typeof DataView.prototype.getFloat16 === "function" first.
About 3 significant decimal digits. 0.1 reads back as 0.0999755859375, and above 2048 only even whole numbers can be stored.
65504, apart from Infinity. Larger values cannot be stored in a 16-bit float.
When the data was saved as half floats, for example GPU textures or compressed machine-learning weights. For general numbers getFloat32() or getFloat64() are more precise.

Next: getFloat32()

Read a 4-byte float, the common choice for sensor values and 3D graphics.

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