JavaScript Reference: Number
By Flavio
Published · Updated
Tagged: JavaScript
Complete reference to JavaScript Number values, static constants and methods (isNaN, isFinite, isInteger, isSafeInteger, parseFloat, parseInt), formatting methods (toFixed, toPrecision, toLocaleString), and BigInt.
This reference explains how JavaScript Number values work and documents every static property, static method, and Number.prototype method in one place.
JavaScript Numbers use the IEEE 754 binary64 format. The same type represents integers, fractions, Infinity, -Infinity, and NaN.
The exact rules are defined in the ECMAScript Number specification.
Create a Number value
The usual way is a number literal:
const age = 36
typeof age // 'number'
You can call Number() to convert another value:
Number('36') // 36
Number('') // 0
Number(true) // 1
Number('hello') // NaN
Number() follows JavaScript’s numeric conversion rules, which can sometimes be surprising. Validate input instead of assuming every conversion succeeded.
Avoid new Number()
Adding new creates a wrapper object, not a Number primitive:
const age = new Number(36)
typeof age // 'object'
age.valueOf() // 36
Wrapper objects behave differently in comparisons and conditionals:
new Number(0) === 0 // false
Boolean(new Number(0)) // true
Use Number primitives unless you have a very unusual reason to create a wrapper object.
Static properties
The Number constructor provides these constants:
Number.EPSILONis the difference between 1 and the smallest Number greater than 1Number.MAX_SAFE_INTEGERis2 ** 53 - 1, the largest safe integerNumber.MIN_SAFE_INTEGERis-(2 ** 53 - 1), the smallest safe integerNumber.MAX_VALUEis the largest positive finite NumberNumber.MIN_VALUEis the smallest positive nonzero Number, not the most negative NumberNumber.NaNis the special not-a-number valueNumber.NEGATIVE_INFINITYis negative infinityNumber.POSITIVE_INFINITYis positive infinity
Number.EPSILON // 2.220446049250313e-16
Number.MAX_SAFE_INTEGER // 9007199254740991
Number.MIN_SAFE_INTEGER // -9007199254740991
Number.MAX_VALUE // 1.7976931348623157e+308
Number.MIN_VALUE // 5e-324
Number.NaN // NaN
Number.NEGATIVE_INFINITY // -Infinity
Number.POSITIVE_INFINITY // Infinity
Number.EPSILON is not the spacing between every pair of Numbers. Floating-point spacing grows with the magnitude of the values.
Static methods
These methods test or parse values without the type-coercion pitfalls of the older global helpers.
Number.isNaN(value)
Returns true only when value is actually the number NaN. Unlike the global isNaN(), it never converts strings or other types first:
Number.isNaN(NaN) // true
Number.isNaN(0 / 0) // true
Number.isNaN('hello') // false (global isNaN('hello') returns true)
Number.isNaN(undefined) // false
Number.isFinite(value)
Returns true only when value is a number and not Infinity, -Infinity, or NaN. Unlike global isFinite(), it does not coerce strings:
Number.isFinite(10) // true
Number.isFinite('10') // false (global isFinite('10') returns true)
Number.isFinite(Infinity) // false
Number.isFinite(NaN) // false
Number.isInteger(value)
Returns true when value is a finite number with no fractional part:
Number.isInteger(10) // true
Number.isInteger(10.0) // true
Number.isInteger(10.5) // false
Number.isInteger('10') // false
Number.isInteger(Infinity) // false
Number.isSafeInteger(value)
Returns true when value is an integer between -(2 ** 53 - 1) and 2 ** 53 - 1 where consecutive integers remain uniquely representable:
Number.isSafeInteger(9007199254740991) // true (Number.MAX_SAFE_INTEGER)
Number.isSafeInteger(9007199254740992) // false (2 ** 53)
Number.MAX_SAFE_INTEGER + 1 === Number.MAX_SAFE_INTEGER + 2 // true
Use BigInt (9007199254740993n) whenever you need exact arithmetic beyond Number.MAX_SAFE_INTEGER.
Number.parseFloat(string)
Parses a floating-point number from the start of a string and stops at the first non-numeric character. Identical to the global parseFloat():
Number.parseFloat('12.5px') // 12.5
Number.parseFloat(' 3.14') // 3.14
Number.parseFloat('abc') // NaN
Number.parseInt(string, radix)
Parses an integer from the start of a string in the given base (radix, from 2 to 36). Always pass the radix explicitly (usually 10):
Number.parseInt('42px', 10) // 42
Number.parseInt('101', 2) // 5
Number.parseInt('ff', 16) // 255
Number.parseInt('hello', 10) // NaN
Use BigInt for larger integers
When you need arbitrary-size integer arithmetic, use BigInt:
const count = 9007199254740993n
const next = count + 1n
next // 9007199254740994n
BigInt is a separate primitive type. You cannot mix a BigInt and a Number directly in arithmetic (1n + 1 throws a TypeError). Convert deliberately when needed, and remember that converting a large BigInt to Number can lose precision.
Number prototype methods
Number primitives can call methods on Number.prototype directly — JavaScript temporarily boxes the primitive so you never need new Number(). All formatting methods return strings and leave the original number untouched.
.toFixed(digits)
Formats a number using fixed-point notation with digits places after the decimal point (0 to 100, default 0):
;(12.5).toFixed(2) // '12.50'
;(1234.567).toFixed(2) // '1234.57'
;(1234.567).toFixed(0) // '1235'
Note that toFixed() formats using binary floating-point values, so round currency in integer cents before formatting if exact decimal rounding matters.
.toPrecision(precision)
Formats a number to precision total significant digits (1 to 100), switching to exponential notation when needed:
;(1234.567).toPrecision(4) // '1235'
;(1234.567).toPrecision(2) // '1.2e+3'
;(0.001234).toPrecision(2) // '0.0012'
.toExponential(fractionDigits)
Returns a string in scientific (exponential) notation:
;(1234.567).toExponential(2) // '1.23e+3'
;(0.00045).toExponential(1) // '4.5e-4'
.toLocaleString(locales, options)
Formats a number according to locale conventions (thousands separators, decimal symbols, currency, percentages), powered by Intl.NumberFormat:
;(1234567.89).toLocaleString('en-US') // '1,234,567.89'
;(1234567.89).toLocaleString('it-IT') // '1.234.567,89'
;(49.9).toLocaleString('en-US', { style: 'currency', currency: 'USD' }) // '$49.90'
.toString(radix)
Converts a number to a string in base radix (2 through 36, default 10):
;(255).toString() // '255'
;(255).toString(16) // 'ff'
;(10).toString(2) // '1010'
.valueOf()
Returns the underlying primitive number from a Number wrapper object. JavaScript calls this automatically during numeric coercion:
const wrapped = new Number(42)
wrapped.valueOf() // 42
typeof wrapped.valueOf() // 'number'Want me to talk about your product? You can sponsor this site.
Related posts about js: