JavaScript DataView setFloat32() Method

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

Definition

setFloat32() writes a number into four bytes of a DataView as a 32-bit floating point value (single precision). The number is rounded to about 7 significant digits, so it uses half the space of a normal JavaScript Number while staying accurate enough for most measurements. Pass the byte offset, the value and, for little-endian formats, true.

Remember
const view = new DataView(new ArrayBuffer(4));
view.setFloat32(0, 1.5);     // undefined
view.getFloat32(0);          // 1.5
view.setFloat32(0, 21.7);
view.getFloat32(0);          // 21.700000762939453
view.setFloat32(2, 1);       // RangeError

Syntax

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

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

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

// returns undefined; writes 4 bytes starting at byteOffset
// value is converted to a 32-bit floating point number
// needs byteOffset + 4 <= 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 4 bytes must fit: byteOffset + 4 may not be larger than view.byteLength. A negative or too-large offset throws a RangeError.
valueThe number to store. It is rounded to the nearest 32-bit floating point number, so 0.1 comes back as 0.10000000149011612. Values beyond about ±3.4e38 become Infinity or -Infinity.
littleEndianOptional. Leave it out (or pass false) for big-endian: the most significant of the 4 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
undefinedsetFloat32() gives nothing back. Its result is the 4 bytes it changed inside the buffer; read them back with getFloat32().

JavaScript Examples

Example 1 — Save Temperatures in 4 and 8 Bytes

Write each temperature once with setFloat32() and once with setFloat64(), and compare what comes back.

JavaScript
const readings = [21.7, -3.2, 100];
const f32 = new DataView(new ArrayBuffer(4));
const f64 = new DataView(new ArrayBuffer(8));

readings.forEach(function (t) {
  f32.setFloat32(0, t);
  f64.setFloat64(0, t);
  console.log(t + ": float32 " + f32.getFloat32(0) + ", float64 " + f64.getFloat64(0));
});
Try It Yourself

How It Works

  1. Same reading, two sizes. Each temperature is written once as a 4-byte float and once as an 8-byte float.
  2. Float32 shows its rounding. 21.7 comes back as 21.700000762939453: correct to about 7 digits, which is more than any thermometer measures.
  3. Whole numbers like 100 are exact. Small integers and simple fractions fit perfectly in both sizes, so 100 is unchanged.

Example 2 — Write a 7-Byte Alert Packet

Build a packet with a 1-byte type, a 2-byte length and a 4-byte float value, then print its bytes.

JavaScript
const view = new DataView(new ArrayBuffer(7));
view.setUint8(0, 2);            // type: temperature alert
view.setUint16(1, 4);           // length of the value
view.setFloat32(3, -12.5);      // the value

const hex = Array.from(new Uint8Array(view.buffer), function (b) {
  return b.toString(16).padStart(2, "0");
}).join(" ");
console.log(hex);   // 02 00 04 c1 48 00 00
Try It Yourself

How It Works

  1. Plan the layout. The packet is 1 byte of type, 2 bytes of length and 4 bytes of float: 7 bytes in total.
  2. Fill the fields in order. setUint8(0), setUint16(1) and setFloat32(3) each write their own bytes, so one view builds the whole packet.
  3. The float in the dump. The last 4 bytes, c1 48 00 00, are -12.5 in big-endian single precision.

Example 3 — Where a 32-bit Float Runs Out

Write two large integers and two huge numbers, and see which ones survive unchanged.

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

[16777216, 16777217, 3.4e38, 1e39].forEach(function (x) {
  view.setFloat32(0, x);
  console.log(String(x) + " -> " + view.getFloat32(0));
});
Try It Yourself

How It Works

  1. Integers up to 16777216 are safe. A float32 has 24 bits of precision, so every whole number up to 2 to the power 24 (16777216) is exact.
  2. The next integer is not. 16777217 cannot be stored, so setFloat32() rounds it to 16777216.
  3. Huge values: rounded or Infinity. 3.4e38 is close to the maximum and is stored slightly rounded; 1e39 is beyond the float32 range and becomes Infinity.

Browser & Runtime Support

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

✓ ES2015 · Baseline

DataView.prototype.setFloat32()

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

It rounds a number to the nearest 32-bit float and writes those four bytes at the offset. It returns undefined.
A 32-bit float keeps about 7 digits. The stored value is the closest float32 to 21.7, and reading it as a 64-bit Number shows the tiny difference.
About 3.4028234663852886e38. Larger values are stored as Infinity.
When space matters and 7 digits are enough, for example large arrays of sensor readings, audio or 3D vertex data, or when a file format requires 4-byte floats.
It converts the value to a Number first, so "2.5" is stored as 2.5. A string that is not a number becomes NaN.

Next: setFloat64()

Write an 8-byte float that keeps every bit of a JavaScript Number.

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