W3docs

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 like 42 and decimals like 3.14. There is no separate int or float.
  • BigInt — for integers beyond the safe range of number (more on that below). A BigInt literal ends in n, e.g. 9007199254740993n.

You can tell them apart with typeof:

javascript— editable

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:

javascript— editable

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:

  1. Math.floor(): Rounds a number down to the nearest integer.
javascript— editable
  1. Math.ceil(): Rounds a number up to the nearest integer.
javascript— editable
  1. Math.round(): Rounds a number to the nearest integer, following standard mathematical rules.
javascript— editable
  1. Math.trunc(): Removes any fractional digits, essentially truncating the number.
javascript— editable

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.
javascript— editable

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.

javascript— editable

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:

javascript— editable

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 is NaN. Note that the global isNaN() performs implicit type coercion, so isNaN('hello') returns true. For strict type checking, use Number.isNaN(), which only returns true if the value is actually NaN.
  • isFinite(): Checks whether a value is a finite number, returning false for Infinity, -Infinity, or NaN.

Examples:

  • isNaN('hello') returns true because the string coerces to NaN.
  • isFinite(2 / 0) returns false, as 2 / 0 results in Infinity, 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.

javascript— editable

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.

javascript— editable

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:

javascript— editable

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.

Practice

Practice
What is true about JavaScript numbers?
What is true about JavaScript numbers?
Was this page helpful?