JavaScript DataView setFloat64() Method

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

Definition

setFloat64() writes a number into eight bytes of a DataView as a 64-bit floating point value (double precision). Since every JavaScript Number is already stored this way, nothing is rounded: the exact bit pattern goes into the buffer. That makes setFloat64() the safe choice for saving measurements, calculation results or any value you must read back identically. Pass true as the third argument for little-endian order.

Remember
const view = new DataView(new ArrayBuffer(8));
view.setFloat64(0, 0.1);    // undefined
view.getFloat64(0);         // 0.1
view.getFloat32(0);         // 1.4499999284744263
view.setFloat64(4, 1);      // RangeError

Syntax

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

JavaScript
dataview.setFloat64(byteOffset, value)
dataview.setFloat64(byteOffset, value, littleEndian)
CallWhat it does
view.setFloat64(byteOffset, value)Converts value to a 64-bit floating point number (double precision) and writes it into the 8 bytes starting at byteOffset, big-endian. Returns undefined.
view.setFloat64(byteOffset, value, true)Writes the same 8 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 Float64Array, a DataView has no alignment rule: setFloat64() works at any byte position, even one that is not a multiple of 8.

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

// returns undefined; writes 8 bytes starting at byteOffset
// value is converted to a 64-bit floating point number
// needs byteOffset + 8 <= 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 8 bytes must fit: byteOffset + 8 may not be larger than view.byteLength. A negative or too-large offset throws a RangeError.
valueThe number to store. Every JavaScript Number is already a 64-bit float, so it is stored exactly, including NaN, Infinity and -0.
littleEndianOptional. Leave it out (or pass false) for big-endian: the most significant of the 8 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
undefinedsetFloat64() gives nothing back. Its result is the 8 bytes it changed inside the buffer; read them back with getFloat64().

JavaScript Examples

Example 1 — Compare the Bytes of 0.1 + 0.2 and 0.3

Write both numbers as doubles and compare their raw bytes to see the famous difference.

JavaScript
const view = new DataView(new ArrayBuffer(16));
view.setFloat64(0, 0.1 + 0.2);
view.setFloat64(8, 0.3);

const bytes = Array.from(new Uint8Array(view.buffer), function (b) {
  return b.toString(16).padStart(2, "0");
});
console.log(bytes.slice(0, 8).join(" "));   // 3f d3 33 33 33 33 33 34
console.log(bytes.slice(8).join(" "));      // 3f d3 33 33 33 33 33 33
console.log(view.getFloat64(0) === view.getFloat64(8));   // false
Try It Yourself

How It Works

  1. Two numbers that look equal. In JavaScript 0.1 + 0.2 is 0.30000000000000004, not exactly 0.3; both are written as doubles.
  2. Compare the raw bytes. The two 8-byte patterns are identical except for the very last bit: 34 versus 33.
  3. The difference is real. Reading both back with getFloat64() and comparing them gives false, which is why decimals should be compared with a small tolerance.

Example 2 — Save the Speed of Light Little-Endian

Write a physical constant with true, then read it back in both byte orders.

JavaScript
const c = 299792458;               // metres per second
const view = new DataView(new ArrayBuffer(8));
view.setFloat64(0, c, true);

console.log(view.getFloat64(0, true) === c);   // true
console.log(view.getFloat64(0) === c);         // false
console.log(view.getUint8(7).toString(16));    // 41
Try It Yourself

How It Works

  1. A constant saved little-endian. The speed of light, 299792458, is written with true, so the least significant byte goes to offset 0.
  2. Same order, same number. getFloat64(0, true) returns exactly 299792458; reading big-endian gives something else entirely.
  3. The sign and exponent sit at the end. In little-endian order the most significant byte, 41, is the last one (offset 7).

Example 3 — Store -0 and Infinity Exactly

Doubles have special values. Write -0 and Infinity and print their bit patterns as two 32-bit hex halves.

JavaScript
const view = new DataView(new ArrayBuffer(8));
const show = function () {
  return view.getUint32(0).toString(16).padStart(8, "0") + " " + view.getUint32(4).toString(16).padStart(8, "0");
};

view.setFloat64(0, -0);
console.log(show());                              // 80000000 00000000
console.log(Object.is(view.getFloat64(0), -0));   // true

view.setFloat64(0, Infinity);
console.log(show());                              // 7ff00000 00000000
Try It Yourself

How It Works

  1. Negative zero is real. JavaScript has both 0 and -0. setFloat64(0, -0) stores only the sign bit, giving 80000000 00000000.
  2. It survives the round trip. Object.is() can tell -0 from 0, and it confirms that getFloat64() returns -0 again.
  3. Infinity has a fixed pattern. All exponent bits on and a zero fraction: 7ff00000 00000000.

Browser & Runtime Support

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

✓ ES2015 · Baseline

DataView.prototype.setFloat64()

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

No. JavaScript Numbers are already 64-bit doubles, so setFloat64() stores their exact bits.
Because they are different Numbers: 0.1 + 0.2 is 0.30000000000000004. setFloat64() faithfully stores that tiny difference in the last bit.
Yes. Each has its own bit pattern, and getFloat64() returns the same special value.
Eight, starting at the byte offset. The offset plus 8 must fit inside the view.
When about 7 significant digits are enough and you want to halve the size. For exact round trips of JavaScript Numbers, use setFloat64().

Next: setInt16()

Write a signed 2-byte whole number, such as a WAV audio sample.

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