J

JavaScript Handbook

Clean • Professional

Number Reference / Utilities

2 minute

Number Reference / Utilities

JavaScript provides the Number object, which includes properties and methods to work with numeric values. These utilities are divided into static properties/methods (accessed via Number) and instance methods (called on number values).

Note: All number methods return a new value. They do not modify the original number.

Number Properties

These are static properties of the Number object, accessed as Number.<property>.

PropertyDescriptionExample
constructorReturns the function that created the Number prototype (usually Number)(123).constructor === Number; // true
EPSILONSmallest difference between 1 and the next representable numberNumber.EPSILON; // 2.220446049250313e-16
MAX_VALUELargest finite number in JavaScriptNumber.MAX_VALUE; // 1.7976931348623157e+308
MIN_VALUESmallest positive number greater than 0Number.MIN_VALUE; // 5e-324
MAX_SAFE_INTEGERLargest integer that can be safely representedNumber.MAX_SAFE_INTEGER; // 9007199254740991
MIN_SAFE_INTEGERSmallest integer that can be safely representedNumber.MIN_SAFE_INTEGER; // -9007199254740991
POSITIVE_INFINITYRepresents positive infinityNumber.POSITIVE_INFINITY; // Infinity
NEGATIVE_INFINITYRepresents negative infinityNumber.NEGATIVE_INFINITY; // -Infinity
NaNRepresents “Not-a-Number”Number.NaN; // NaN
prototypeAllows adding properties or methods to all Number instances (use cautiously)Number.prototype.customMethod = function() { … }

Static Number Methods

These are called on the Number object itself:

MethodDescriptionExample
Number.isFinite(value)Returns true if the value is finite (not Infinity, -Infinity, or NaN)Number.isFinite(123); // true
Number.isInteger(value)Returns true if the value is an integerNumber.isInteger(42.5); // false
Number.isNaN(value)Returns true if the value is exactly NaNNumber.isNaN(NaN); // true
Number.isSafeInteger(value)Returns true if value is a safe integerNumber.isSafeInteger(9007199254740992); // false
Number.parseFloat(string)Parses a string and returns a floating-point numberNumber.parseFloat("3.14"); // 3.14
Number.parseInt(string, [radix])Parses a string and returns an integerNumber.parseInt("1010", 2); // 10

Instance Number Methods

These methods are called on number values or Number objects:

MethodDescriptionExample
toExponential(fractionDigits)Converts a number to exponential notation(1234).toExponential(2); // "1.23e+3"
toFixed(digits)Formats a number with a fixed number of decimal places(3.14159).toFixed(2); // "3.14"
toLocaleString([locales, options])Converts a number to a string formatted according to locale(123456.789).toLocaleString('en-US'); // "123,456.789"
toPrecision(precision)Formats a number to a specified total number of significant digits(123.456).toPrecision(4); // "123.5"
toString([radix])Converts a number to a string in the specified base(255).toString(16); // "ff"
valueOf()Returns the primitive numeric valuenew Number(42).valueOf(); // 42

 

Article 0 of 0