Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

Float32Array: 32-bit floating-point values

Float32Array stores Number values as 32-bit IEEE 754 numbers, using 4 bytes per element.

Version: ECMAScript 2015 onward; example target: Node.js 24.22.2

Syntax

new Float32Array(); new Float32Array(length); new Float32Array(source); new Float32Array(buffer[, byteOffset[, length]])

new Float32Array(source?, byteOffset?, length?) creates a numeric typed array. The source can be a length, iterable or array-like value, another numeric typed array, or an ArrayBuffer/SharedArrayBuffer; the buffer form is a view without copying. byteOffset must be a multiple of 4, and BYTES_PER_ELEMENT is 4.

On assignment, a value goes through ToNumber and is rounded to the nearest representable binary32 value. Thus 0.1 changes slightly and 16777217 becomes 16777216; NaN, infinities, and the sign of -0 are supported. Assigning a BigInt or using a BigInt typed array as source throws TypeError. Byte order is native; use DataView for a fixed format.

Length goes through ToIndex: fractions truncate toward zero, NaN/undefined become 0, and -0.5 also gives 0. A negative result after ToIntegerOrInfinity, infinity, a misaligned offset, or a range beyond the buffer throws RangeError; a detached buffer throws TypeError. The type has been standard since ECMAScript 2015.

Examples

const values = new Float32Array([0.1, 16777217, -0, Infinity]);
console.log(values[0]);
console.log(values[1]);
console.log(Object.is(values[2], -0), values[3]);
console.log(Float32Array.BYTES_PER_ELEMENT, new Float32Array(2.9).length, new Float32Array(-0.5).length);
try { values[0] = 1n; } catch (error) { console.log(error instanceof TypeError); }

Result

0.10000000149011612
16777216
true Infinity
4 2 0
true

Sources