ES6 - Number
ES6 - Number Object and Numeric Literals
Section titled “ES6 - Number Object and Numeric Literals”In JavaScript, numbers are primarily primitive values. However, JavaScript also provides a Number object, which is a wrapper for primitive number values. You typically don’t need to create Number objects directly, as JavaScript automatically converts number literals to Number instances when methods are called on them.
const numObj = new Number(value);If value cannot be converted to a number, the Number object will represent NaN (Not-a-Number). It’s generally better to work with number primitives.
Static Properties of the Number Object
Section titled “Static Properties of the Number Object”These properties are accessed directly on the Number constructor (e.g., Number.MAX_VALUE).
| Property | Description |
|---|---|
Number.EPSILON | The smallest difference between two representable numbers. Useful for floating-point comparisons. |
Number.MAX_SAFE_INTEGER | The maximum safe integer in JavaScript (253 - 1). Operations beyond this may lose precision. |
Number.MIN_SAFE_INTEGER | The minimum safe integer in JavaScript (-(253 - 1)). |
Number.MAX_VALUE | The largest positive representable number. |
Number.MIN_VALUE | The smallest positive representable number (closest to zero, but not zero). |
Number.NaN | Represents “Not-a-Number”, a special value indicating an unrepresentable or undefined numerical result. |
Number.NEGATIVE_INFINITY | Special value representing negative infinity (e.g., result of dividing a negative number by zero). |
Number.POSITIVE_INFINITY | Special value representing positive infinity (e.g., result of dividing a positive number by zero). |
Number.prototype | An object that allows you to add properties and methods to all Number instances. (For advanced use). |
Static Methods of the Number Object
Section titled “Static Methods of the Number Object”These methods are called directly on the Number constructor.
| Method | Description |
|---|---|
Number.isNaN(value) | Determines if the passed value is NaN. More robust than the global isNaN() function, as it doesn’t coerce the argument to a number. |
Number.isFinite(value) | Determines if the passed value is a finite number (not NaN, Infinity, or -Infinity). |
Number.isInteger(value) | Determines if the passed value is an integer. |
Number.isSafeInteger(value) | Determines if the passed value is a safe integer (i.e., within the range Number.MIN_SAFE_INTEGER to Number.MAX_SAFE_INTEGER). |
Number.parseFloat(string) | Parses a string argument and returns a floating-point number. Same as the global parseFloat(). |
Number.parseInt(string, radix) | Parses a string argument and returns an integer of the specified radix (base). Same as the global parseInt(). |
console.log(Number.isNaN(NaN)); // trueconsole.log(Number.isNaN('hello' / 2)); // trueconsole.log(Number.isFinite(123)); // trueconsole.log(Number.isFinite(Infinity)); // falseconsole.log(Number.isInteger(10)); // trueconsole.log(Number.isInteger(10.5)); // falseconsole.log(Number.isSafeInteger(Math.pow(2, 53) - 1)); // trueconsole.log(Number.isSafeInteger(Math.pow(2, 53))); // falseInstance Methods of Number
Section titled “Instance Methods of Number”These methods are called on number primitives or Number objects.
| Method | Description |
|---|---|
num.toExponential(fractionDigits) | Returns a string representing the number in exponential notation. |
num.toFixed(digits) | Returns a string representing the number in fixed-point notation (with a specified number of digits after the decimal point). |
num.toLocaleString(locales, options) | Returns a string with a language-sensitive representation of the number. |
num.toPrecision(precision) | Returns a string representing the number to a specified precision (significant digits). |
num.toString(radix) | Returns a string representing the number in the specified radix (base, e.g., 2 for binary, 16 for hexadecimal). Defaults to base 10. |
num.valueOf() | Returns the primitive value of the Number object. |
const n = 12345.6789;console.log(n.toExponential(2)); // "1.23e+4"console.log(n.toFixed(2)); // "12345.68"console.log(n.toPrecision(7)); // "12345.68"console.log((255).toString(16)); // "ff" (hexadecimal)console.log((10).toString(2)); // "1010" (binary)Numeric Literals (ES6+)
Section titled “Numeric Literals (ES6+)”ES6 introduced convenient syntax for binary and octal integer literals. Hexadecimal literals were already available.
- Binary Literals: Prefix with
0bor0B. Example:0b1010(decimal 10). - Octal Literals: Prefix with
0oor0O. Example:0o12(decimal 10). (Note: Old octal literals starting with just0are deprecated in strict mode and should be avoided). - Hexadecimal Literals: Prefix with
0xor0X. Example:0xA(decimal 10).
console.log(0b001); // 1console.log(0b010); // 2console.log(0b1111); // 15console.log(0o010); // 8console.log(0o100); // 64console.log(0xFF); // 255 (F is 15 in hex, FF = 15*16 + 15)console.log(0x100); // 256 (1*16^2 + 0*16^1 + 0*16^0)BigInt (ES2020)
Section titled “BigInt (ES2020)”For numbers larger than Number.MAX_SAFE_INTEGER or smaller than Number.MIN_SAFE_INTEGER, JavaScript introduced the BigInt type. BigInts are created by appending n to the end of an integer literal or by calling the BigInt() constructor.
const largeNumber = 9007199254740991n; // A BigIntconst anotherLarge = BigInt("9007199254740992");console.log(largeNumber + 1n); // 9007199254740992n// Note: You cannot mix Number and BigInt types in standard arithmetic operations directly.// console.log(largeNumber + 1); // This would throw a TypeErrorBigInts allow for arbitrary-precision integer arithmetic. For more details, see the MDN BigInt documentation.