How Numbers Work in JavaScript and IEEE 754 Doubles
Every JavaScript number is a 64-bit double with 53 bits of precision. See its layout, the safe integer limit, Infinity, rounding gaps and the 32-bit trap.

JavaScript has one number type, and it is the same 64-bit floating point format every modern CPU implements for double. That single fact explains the safe integer limit, why 0.1 + 0.2 is not 0.3, why 1 / 0 is a value and not an error and why a bitwise operation on a large number returns nonsense. It is worth knowing the layout once, because the rest of the number behavior is the consequence. This post shows the bit layout using real output from Node.js 24, the limits worth memorizing, the gaps between representable values and the 32-bit trap in bitwise operators. The decimal rounding side is covered in why 0.1 + 0.2 is not 0.3, and the larger-integer alternative in the BigInt post.
The layout
A double is 64 bits: one for the sign, eleven for the exponent, and 52 for the fraction. The value is the sign, times one plus the fraction, times two to the exponent, with a bias. A typed array lets you look at the bits directly.
function bits(value) {
const view = new DataView(new ArrayBuffer(8));
view.setFloat64(0, value);
return view.getBigUint64(0).toString(2).padStart(64, "0");
}
function show(value) {
const b = bits(value);
console.log(String(value).padEnd(6), b[0], b.slice(1, 12), b.slice(12));
}
show(1);
show(-2);
show(0.5);
show(0.1);1 0 01111111111 0000000000000000000000000000000000000000000000000000
-2 1 10000000000 0000000000000000000000000000000000000000000000000000
0.5 0 01111111110 0000000000000000000000000000000000000000000000000000
0.1 0 01111111011 1001100110011001100110011001100110011001100110011010The columns are sign, exponent and fraction. The number 1 has a zero fraction and an exponent of 1023, the bias, which is 01111111111 in binary. Negating flips the first bit, and -2 also raises the exponent by one. The pattern in the fraction of 0.1 is the repeating 1001 that cannot terminate, ending in a rounded ...1010. A finite number of bits holds it, so the stored value is an approximation.
Whole numbers and the safe limit
An integer is exact in a double as long as it fits in 53 bits, because the 52 stored fraction bits plus an implied leading one give 53 bits of precision. The largest such integer is 2**53 - 1.
console.log(Number.MAX_SAFE_INTEGER);
console.log(Number.MIN_SAFE_INTEGER);
console.log(Number.isSafeInteger(2 ** 53 - 1));
console.log(Number.isSafeInteger(2 ** 53));
console.log(Number.isInteger(5.0));
console.log(5.0 === 5);
console.log(9007199254740993);9007199254740991
-9007199254740991
true
false
true
true
90071992547409925.0 and 5 are the same value, because there is no separate integer type. The last line shows the failure: the literal 9007199254740993 is not representable, so the engine stores the nearest double, 9007199254740992, and prints that. Nothing warns you. This is why ids from databases and APIs that exceed the limit must travel as strings or bigint.
Beyond the limit, representable doubles are further apart, and the gap doubles each time the exponent steps up.
const big = 2 ** 53;
console.log(big + 1);
console.log(big + 2);
console.log(big + 3);
console.log(Number.MAX_SAFE_INTEGER + 2);
console.log(2 ** 54 + 1);
console.log(2 ** 54 + 2);9007199254740992
9007199254740994
9007199254740996
9007199254740992
18014398509481984
18014398509481984Between 2**53 and 2**54, only even integers exist. big + 1 falls between two neighbors and rounds to the even one, which is big itself, so adding one changed nothing. big + 3 sits halfway between two representable values, and ties go to the one whose last bit is zero. Between 2**54 and 2**55, the spacing is four, so both + 1 and + 2 round back. A loop for (let i = 2 ** 53; i < 2 ** 53 + 5; i++) never terminates for exactly this reason.
Extremes: overflow and underflow
The format has special values rather than exceptions. Overflow gives Infinity, and a result too small to represent becomes a subnormal and eventually 0.
console.log(Number.MAX_VALUE);
console.log(Number.MAX_VALUE * 2);
console.log(Number.MIN_VALUE);
console.log(Number.MIN_VALUE / 2);
console.log(1 / 0, -1 / 0, 0 / 0);
console.log(Infinity - Infinity);
console.log(Number.isFinite(Infinity));1.7976931348623157e+308
Infinity
5e-324
0
Infinity -Infinity NaN
NaN
falseNumber.MAX_VALUE is about 1.8 times ten to the 308th, and doubling it gives Infinity. Number.MIN_VALUE is the smallest positive subnormal, 5e-324, and halving it rounds to zero. Division by zero is Infinity and not an error, which is a difference from integer arithmetic in most languages. Operations that have no defined answer, such as 0 / 0 and Infinity - Infinity, give NaN, covered in the post on NaN.
Bitwise operators work on 32 bits
The operators |, &, ^, ~, << and >> convert their operands to signed 32-bit integers, do the operation and convert back. That makes them quick for flags and wrong for large values.
console.log(2 ** 31 | 0);
console.log(2 ** 32 | 0);
console.log(2 ** 32 + 5 | 0);
console.log(1 << 31);
console.log(1 << 32);
console.log(~~3.7);
console.log(Math.trunc(-3.7), ~~-3.7);
console.log(5 >>> 0, -1 >>> 0);-2147483648
0
5
-2147483648
1
3
-3 -3
5 4294967295The first three lines show wraparound: 2**31 becomes negative, 2**32 becomes zero and 2**32 + 5 becomes 5. A shift count is taken modulo 32, so 1 << 32 is 1. ~~x truncates toward zero for numbers in the 32-bit range, matching Math.trunc(), and breaks outside it. The unsigned shift >>> 0 reinterprets the 32 bits as unsigned, giving 4294967295 for -1. For anything that can exceed 2^31, use arithmetic or bigint. The post on bitwise and shift operators has the practical cases.
Reading and writing numbers as text
Number literals and conversions have several forms, and toString takes a radix.
console.log(255..toString(16));
console.log((255).toString(2));
console.log((0.5).toString(2));
console.log(parseInt("ff", 16));
console.log(0xff, 0b1010, 0o17, 1e3, 1_000_000);
console.log((1234.5678).toFixed(1), (1234.5678).toPrecision(6));ff
11111111
0.1
255
255 10 15 1000 1000000
1234.6 1234.57The double dot in 255..toString(16) is a syntax detail: the first dot belongs to the number, and the second is the member access. Binary, octal and hex literals are all accepted, and _ is a numeric separator, covered in the post on numeric literals. toFixed() and toPrecision() return strings.
Checking where a value sits
Two helpers answer most "can this number be trusted" questions at a boundary. Number.isSafeInteger() says an integer is exact, and Number.isFinite() rules out Infinity and NaN. Combined, they make a short validator for ids and counts.
function isExactCount(value) {
return Number.isSafeInteger(value) && value >= 0;
}
console.log(isExactCount(42));
console.log(isExactCount(-1));
console.log(isExactCount(1.5));
console.log(isExactCount(2 ** 53));
console.log(isExactCount(Number.MAX_SAFE_INTEGER));
console.log(isExactCount("42"));true
false
false
false
true
falseNeither function converts strings, so "42" fails and the caller has to convert first, which is the habit we want. The value 2**53 is rejected even though it is a perfectly valid double, because it is the first integer that cannot be told apart from its neighbor. An id above the limit should be treated as a string, since arithmetic on it cannot be trusted and you never need to do arithmetic on an id.
What this means for your code
Four rules cover most of it. Treat integers above 2^53 as a different problem and reach for bigint or strings. Never compare computed decimals with ===. Never use bitwise operators on values that might be large. And expect Infinity and NaN to flow through arithmetic and check for them with Number.isFinite() at boundaries.
Performance is a separate matter, and one that people often reach for wrongly. Engines represent small integers specially, so integer loops are fast, but you cannot observe or control that, and writing code to appease it is a poor trade. Measure before changing anything.
A note on typed arrays and other number formats
The double is the only number type the language gives you for ordinary arithmetic, but typed arrays store other formats and convert on every read and write. A Uint8Array wraps values modulo 256, so storing 300 keeps 44. A Float32Array rounds to a 32-bit float and returns a double with the rounded value, which is why reading back 0.1 gives 0.10000000149011612. These conversions are not errors, and they are the source of puzzling output when data moves between a typed buffer and ordinary numbers. Keep the storage type in mind whenever a number has been through a buffer, and see typed arrays and ArrayBuffer for the full list.
Where this model stops
If you need arbitrary precision, use bigint for integers or a decimal library for fractions. If you need to interoperate with 32-bit integers, such as a binary file format, use DataView or typed arrays, which give you exact integer types and endianness. And Float32Array gives you 32-bit floats when a file format or a GPU buffer requires them, with Math.fround() to round a value to that precision.
The format is specified in the language specification under the Number type, and MDN's page for Number lists the constants and methods used above.


