JavaScript Numbers
JavaScript, as a dynamic programming language, supports various types of numbers. Understanding these numbers is crucial for effective coding. This article
Introduction to JavaScript Numbers
JavaScript is a dynamic language, and the way it stores numbers has direct consequences for everyday code: why 0.1 + 0.2 is not exactly 0.3, why very large integers lose precision, and how to round, format, and parse values safely. This page covers the regular number type, the special values Infinity and NaN, conversion functions, and the gotchas that trip up most developers — with runnable examples you can edit.
Understanding Number Types
JavaScript has exactly two numeric data types:
number— the everyday type. It uses the 64-bit IEEE-754 format ("double-precision floating-point"), so the same type holds both integers like42and decimals like3.14. There is no separateintorfloat.BigInt— for integers beyond the safe range ofnumber(more on that below). A BigInt literal ends inn, e.g.9007199254740993n.
You can tell them apart with typeof:
Writing Numbers in JavaScript
Beyond plain digits, JavaScript offers a few conveniences for readability:
let billion = 1e9; // scientific notation → 1000000000
let ms = 1e-6; // 0.000001 (one microsecond)
let big = 1_000_000; // underscores as visual separators (ES2021)1e9 means "1 followed by 9 zeros"; 1e-6 means "move the decimal 6 places left." The underscores in 1_000_000 are ignored by the engine — they only help humans read large numbers.
Hexadecimal, Binary, and Octal Numbers
JavaScript can read literals in several numeral systems. They are all stored as the same number type — only the notation differs:
let hex = 0xFF; // 255 (base 16, prefix 0x)
let bin = 0b1010; // 10 (base 2, prefix 0b)
let oct = 0o744; // 484 (base 8, prefix 0o)The toString Method
num.toString(base) converts a number to a string in any base from 2 to 36 — the reverse direction from the literals above:
Note: to call a method directly on a literal you need two dots or parentheses — 255..toString(16) or (255).toString(16) — because 255. is read as a decimal point.
Rounding Methods
JavaScript provides several functions for rounding numbers:
Math.floor(): Rounds a number down to the nearest integer.
Math.ceil(): Rounds a number up to the nearest integer.
Math.round(): Rounds a number to the nearest integer, following standard mathematical rules.
Math.trunc(): Removes any fractional digits, essentially truncating the number.
The difference between Math.floor and Math.trunc only shows up with negatives: Math.floor(-3.5) is -4 (rounds toward -Infinity), while Math.trunc(-3.5) is -3 (just drops the fraction). For more rounding and math helpers, see the JavaScript Math chapter.
Formatting Numbers: toFixed and toPrecision
When you need a number as a string with a fixed shape — money, percentages, reports — use these instead of Math.round:
toFixed(digits)keeps a fixed number of digits after the decimal point.toPrecision(digits)keeps a fixed number of significant digits.
Both return strings. Wrap the result in Number() if you need a number back. toLocaleString() is the go-to for thousands separators and currency in the user's locale.
Handling Imprecise Calculations
Because the number type is binary floating-point, some decimal fractions cannot be stored exactly — the famous 0.1 + 0.2 !== 0.3. This is not a JavaScript bug; every IEEE-754 language has it.
The rule of thumb: never compare floating-point results with ===. Either round both sides first, or check that their difference is smaller than Number.EPSILON (the smallest gap between 1 and the next representable number).
Safe Integers
A number can store integers exactly only up to Number.MAX_SAFE_INTEGER (2^53 − 1). Beyond that, two different integers can collapse to the same value:
If you work with IDs, timestamps in nanoseconds, or large counters that exceed this limit, switch to BigInt.
Special Numeric Values
Understanding Infinity, -Infinity, and NaN in JavaScript:
- Infinity: Represents infinity, a value greater than any other number. You get this result when you divide a number by zero or when you exceed the upper limit of the floating-point numbers.
- -Infinity: Represents negative infinity, a value lower than any other number. This occurs when you divide a negative number by zero or exceed the lower limit of the floating-point numbers.
- NaN: Stands for "Not-a-Number." This value results from an undefined or unrepresentable operation in mathematics, like dividing zero by zero.
isNaN() and isFinite() methods for checking these special values:
isNaN(): Checks whether a value isNaN. Note that the globalisNaN()performs implicit type coercion, soisNaN('hello')returnstrue. For strict type checking, useNumber.isNaN(), which only returnstrueif the value is actuallyNaN.isFinite(): Checks whether a value is a finite number, returningfalseforInfinity,-Infinity, orNaN.
Examples:
isNaN('hello')returnstruebecause the string coerces toNaN.isFinite(2 / 0)returnsfalse, as2 / 0results inInfinity, which is not finite.
Numeric Conversion: Number(), parseInt, and parseFloat
There are three common ways to turn a string into a number, and choosing the right one matters.
Number(value) is strict: the entire string must be a valid number (whitespace is trimmed), otherwise you get NaN. An empty string becomes 0.
parseInt(value, radix) is lenient: it reads from the left and stops at the first character that isn't part of an integer. If the first character can't be parsed, it returns NaN. Always pass the radix (base) to avoid surprises.
parseFloat(value) is like parseInt but understands the decimal point, reading until it hits a character that isn't part of a floating-point number:
Rule of thumb: use Number() when the string should be entirely numeric (e.g. validating form input), and parseInt/parseFloat when you need to pull a number out of text like '100px'.
Conclusion
JavaScript stores all everyday numbers as 64-bit floating-point values, which explains rounding quirks, the safe-integer limit, and why formatting and comparison need care. Knowing when to reach for toFixed, Number.EPSILON, BigInt, or parseInt lets you write code that behaves predictably. Continue with the related type guides: Strings, BigInt, the Math object, and Data Types.