By the end of this lesson, you will be able to explain how JavaScript stores integers and decimals as the same Number type and write code that avoids common precision and range mistakes.
What it is
JavaScript has one primitive numeric type: Number. It represents both integers and decimals using the IEEE 754 double-precision floating-point format. This means 42, 42.0, and 42.5 are all numbers, not separate integer and decimal types. The expression typeof 42 returns "number".
Related terms include safe integers, NaN, Infinity, and BigInt. A safe integer is a whole number that JavaScript can represent exactly within the range from -9007199254740991 to 9007199254740991.
Why it matters
- Most everyday calculations in JavaScript use
Number, so you need to understand its limits. - Decimal arithmetic can produce tiny rounding errors, which matters for money, measurements, and comparisons.
- Large identifiers, such as database IDs, can lose precision if stored as
Number. - Functions like
Number.isIntegerandNumber.isSafeIntegerhelp you validate data. - Choosing between
Number,BigInt, andstringprevents subtle bugs.
Syntax or steps
Write numeric literals directly. JavaScript supports decimal, scientific, and radix notation.
const n = 42;
const pi = 3.14159;
const big = 1e6;
const hex = 0xff;
const binary = 0b1010;
const octal = 0o17;
console.log(typeof n);
The first line creates a whole-number value. The second creates a decimal value. The remaining lines show alternative literal forms that still produce Number values.
Example
const price = 0.1;
const quantity = 3;
const total = price * quantity;
console.log(total);
console.log(total === 0.3);
console.log(Number.isInteger(42));
console.log(Number.isInteger(42.5));
console.log(Number.MAX_SAFE_INTEGER);
This example shows three important behaviors:
totalprints0.30000000000000004, not exactly0.3.total === 0.3printsfalsebecause the stored binary value is slightly different.Number.isInteger(42)printstrue, whileNumber.isInteger(42.5)printsfalse.Number.MAX_SAFE_INTEGERprints9007199254740991, the largest integer that can be represented exactly and safely.
Common mistakes
- Assuming decimals are exact. Fix: compare with a small tolerance, or store money as integer cents.
- Using
Numberfor huge IDs. Fix: usestringfor identifiers, orBigIntwhen arithmetic is required. - Confusing
parseIntwithNumber. Fix: remember thatparseInt("42px")returns42, butNumber("42px")returnsNaN. - Testing
NaNwith equality. Fix: useNumber.isNaN(value), becauseNaN === NaNisfalse.
When to use it
Use Number for ordinary calculations. Use BigInt when you need exact whole numbers beyond the safe integer range. Use string when the value is an identifier rather than a quantity.
| Need | Use | Why |
|---|---|---|
| Counters, prices, measurements | Number | Built-in and sufficient for normal ranges. |
| Exact huge integers | BigInt | Avoids floating-point precision loss. |
| IDs, phone numbers, zip codes | string | Preserves leading zeros and avoids accidental math. |
Practice
Guided exercise: store a price as cents, convert it to dollars, and display two decimal places.
const cents = 1999;
const dollars = cents / 100;
console.log(dollars.toFixed(2));
Expected output: 19.99.
Challenge: write a function that returns true only when a value is a safe whole number.
function isSafeWholeNumber(value) {
return Number.isSafeInteger(value);
}
Solution hint: Number.isSafeInteger checks both that the value is an integer and that it is within the safe range.
Quick check
Question: Why does 0.1 + 0.2 === 0.3 return false in JavaScript?
Answer: JavaScript stores numbers as binary floating-point values, and some decimal fractions cannot be represented exactly.
Summary
JavaScript uses one Number type for both integers and decimals, which makes code simple but requires care with precision and range. For normal calculations, use Number; for exact huge integers, use BigInt; for identifiers, use string.