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>
.
Property | Description | Example |
---|---|---|
constructor | Returns the function that created the Number prototype (usually Number ) | (123).constructor === Number; // true |
EPSILON | Smallest difference between 1 and the next representable number | Number.EPSILON; // 2.220446049250313e-16 |
MAX_VALUE | Largest finite number in JavaScript | Number.MAX_VALUE; // 1.7976931348623157e+308 |
MIN_VALUE | Smallest positive number greater than 0 | Number.MIN_VALUE; // 5e-324 |
MAX_SAFE_INTEGER | Largest integer that can be safely represented | Number.MAX_SAFE_INTEGER; // 9007199254740991 |
MIN_SAFE_INTEGER | Smallest integer that can be safely represented | Number.MIN_SAFE_INTEGER; // -9007199254740991 |
POSITIVE_INFINITY | Represents positive infinity | Number.POSITIVE_INFINITY; // Infinity |
NEGATIVE_INFINITY | Represents negative infinity | Number.NEGATIVE_INFINITY; // -Infinity |
NaN | Represents “Not-a-Number” | Number.NaN; // NaN |
prototype | Allows 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:
Method | Description | Example |
---|---|---|
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 integer | Number.isInteger(42.5); // false |
Number.isNaN(value) | Returns true if the value is exactly NaN | Number.isNaN(NaN); // true |
Number.isSafeInteger(value) | Returns true if value is a safe integer | Number.isSafeInteger(9007199254740992); // false |
Number.parseFloat(string) | Parses a string and returns a floating-point number | Number.parseFloat("3.14"); // 3.14 |
Number.parseInt(string, [radix]) | Parses a string and returns an integer | Number.parseInt("1010", 2); // 10 |
Instance Number Methods
These methods are called on number values or Number objects:
Method | Description | Example |
---|---|---|
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 value | new Number(42).valueOf(); // 42 |