JavaScript has one number type and not two. There is no separate integer type and no separate float type, and all of the arithmetic runs on 64-bit floating-point values. That single decision explains the rounding surprises, the safe-integer ceiling, and the reason that BigInt exists at all. Once you understand how the numbers work, most of the "why is this value wrong?" moments in everyday JavaScript disappear.
The previous lesson in this module covered "let, const and var Explained Without Myths", and the next lesson in this module is about "Strings and Quote Styles".
Does JavaScript have separate integer and float types?
No. Every number that you write, whether it is 42, 3.14, or 1e21, becomes the same primitive type called number, and it is stored as a 64-bit floating-point value. One type covers the whole range, which is convenient, and it also means that integers are only exact up to a limit. That limit is one you can reach sooner than you might expect.
console.log(typeof 42); // "number"
console.log(typeof 3.14); // "number"
console.log(typeof 1e21); // "number"
console.log(Number.isInteger(42)); // true
console.log(Number.isInteger(3.0)); // true, because 3.0 is just 3
console.log(Number.isInteger(3.5)); // false
The line worth pausing on is 3.0. There is no way to tell an integer from a float in JavaScript, so Number.isInteger asks a different question. It asks whether the value has no fractional part, and 3.0 passes that test because it is exactly 3.
Why is 0.1 + 0.2 not exactly 0.3?
Because 0.1 and 0.2 cannot be represented exactly in binary floating point. The values that are stored are extremely close to the decimal versions, and adding those two approximations leaves a small remainder of roughly 5.5e-17. That remainder is invisible on paper and very visible in a comparison.
console.log(0.1 + 0.2); // 0.30000000000000004
console.log(0.1 + 0.2 === 0.3); // false
console.log(0.1 + 0.2 - 0.3); // 5.551115123125783e-17
console.log(Math.abs(0.1 + 0.2 - 0.3) < Number.EPSILON); // true
Number.EPSILON is the gap between 1 and the next double that can be represented, and it makes a good tolerance when you compare numbers that came out of arithmetic. The last line prints true.
Two more traps live in the same family. Adding a floating-point number and a whole number usually gives an ugly result, so 0.1 + 0.2 + 1 prints 1.3000000000000003. Division also always produces a float, so 7 / 2 is 3.5 and not 3. If you want the whole part of a division, use Math.trunc or Math.floor and make the name of the variable say so.
What is the largest integer JavaScript can count exactly?
Number.MAX_SAFE_INTEGER is 9007199254740991, which is two to the fifty-third power minus one. Below that number, every whole number has its own unique double representation. Above it, neighbouring whole numbers start collapsing onto the same stored value, and that means a counter can quietly skip a number.
console.log(Number.MAX_SAFE_INTEGER); // 9007199254740991
console.log(Number.MAX_SAFE_INTEGER + 1); // 9007199254740992
console.log(Number.MAX_SAFE_INTEGER + 2); // 9007199254740992, and 2 vanished
console.log(Number.isSafeInteger(9007199254740991)); // true
console.log(Number.isSafeInteger(9007199254740992)); // false
Look at the third and the fourth line. Adding 1 gives an exact result, but adding 2 gives the same answer as adding 1, because the value 9007199254740993 cannot be represented and so it rounds down to ...992. The number did not overflow into an error. It quietly changed, and that is the dangerous part. Number.isSafeInteger is the check to run whenever a value might have come from outside your control, such as a database identifier.
How do I convert a string into a number?
There are three ways to do this, and they behave differently when the input is messy.
console.log(Number("42")); // 42
console.log(Number("42px")); // NaN, because the whole string must be numeric
console.log(Number("")); // 0, because the empty string becomes zero
console.log(parseInt("42px")); // 42, because it stops at the first bad character
console.log(parseInt("0x1f")); // 31, because hex is detected automatically
console.log(parseInt("1f", 16)); // 31, because the radix is given
console.log(parseFloat("3.5kg")); // 3.5
Number() is strict. The entire string has to be a valid number, or you get NaN. parseInt is lenient. It reads the leading digits and throws the rest away. Both behaviours are useful in different places, so use Number() when you want validation and parseInt when you are pulling a number out of the start of a string.
Two rules make parsing safe. Always pass the radix to parseInt, so that parseInt(value, 10) handles ordinary numbers and parseInt(value, 16) handles hex, and the behaviour never depends on what the string happens to start with. And always check the result with Number.isNaN, because NaN is the only value in JavaScript that is not equal to itself, so NaN === NaN is false.
When do I need BigInt?
You need BigInt when the number genuinely has to be exact and genuinely goes past the safe range. Identifiers, cryptographic values, nanosecond timestamps, and large amounts of money in the smallest unit are the usual examples. BigInt is a separate primitive type, and you write the literal with a trailing n.
const big = 9007199254740993n;
console.log(typeof big); // "bigint"
console.log(big); // 9007199254740993n
console.log(big + 1n); // 9007199254740994n
console.log(typeof 9007199254740993); // "number", and it is already imprecise
try {
console.log(big + 1);
} catch (error) {
console.log("mixed:", error.constructor.name, "-", error.message);
}
The fourth line is the one to remember. The plain number literal has already lost a digit before you ever touch BigInt. The last line shows that BigInt will not quietly convert itself, because mixing a BigInt with a Number throws a TypeError with the message Cannot mix BigInt and other types, use explicit conversions. The mistake therefore shows up at the moment it happens and not as a wrong number much later.
BigInt is not a drop-in replacement for number. It has no toFixed, it will not divide, and JSON cannot represent it, so JSON.stringify throws when it meets a BigInt. Use it for the specific values that need exactness, and keep ordinary arithmetic on number.
How do I format a number for display?
Numbers print with as many digits as they need and with no separators, which is rarely what you want to see on a screen.
const amount = 24950.5;
console.log(amount.toFixed(2)); // "24950.50"
console.log(amount.toFixed(0)); // "24951"
console.log(amount.toPrecision(6)); // "24950.5"
console.log((1234567.891).toLocaleString("en-IN")); // "12,34,567.891"
console.log(Math.round(amount)); // 25051
toFixed gives you a fixed number of decimal places and returns a string rather than a number, so remember that when you use it inside arithmetic. toPrecision sets the total number of significant digits, which suits scientific and engineering values. toLocaleString is the one to reach for on a real page, because it applies the grouping and the decimal conventions of a locale, so the same number reads correctly for different audiences.
For real money, the right tool is Intl.NumberFormat. It is the engine behind toLocaleString, and it is far more configurable, because you can fix the currency, force a minimum number of decimals, and control where the separators go.
What to remember about JavaScript numbers
- There is one number type, so integers and floats are the same thing.
Number.isIntegerasks whether a value has a fractional part, and not what type it was written as. - Floating point means that
0.1 + 0.2 !== 0.3, so compare with a tolerance and not with equality. Number.MAX_SAFE_INTEGERis 9007199254740991. Past that point whole numbers start collapsing silently, so check withNumber.isSafeInteger.Number()parses strictly andparseIntparses leniently. Always giveparseInta radix, and always check forNaNwithNumber.isNaN.BigIntis exact past the safe range, but it does not mix withnumberand it cannot be turned into JSON.- Use
toFixedfor fixed decimals,toPrecisionfor significant digits, andIntl.NumberFormatfor anything that a user will read.
Try it yourself: write a script that takes three prices as strings, such as "19.99", "1200", and "7.50". Convert them with Number(), add them together, and print the total with toFixed(2). Now repeat the same thing with parseFloat and with parseInt, and notice which total is right. Then add a fourth price of "abc" and see the difference between a strict NaN and a silent zero. That difference is exactly the bug you want to avoid in a checkout flow.