Skip to content

ES6 - Number

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.

These properties are accessed directly on the Number constructor (e.g., Number.MAX_VALUE).

PropertyDescription
Number.EPSILONThe smallest difference between two representable numbers. Useful for floating-point comparisons.
Number.MAX_SAFE_INTEGERThe maximum safe integer in JavaScript (253 - 1). Operations beyond this may lose precision.
Number.MIN_SAFE_INTEGERThe minimum safe integer in JavaScript (-(253 - 1)).
Number.MAX_VALUEThe largest positive representable number.
Number.MIN_VALUEThe smallest positive representable number (closest to zero, but not zero).
Number.NaNRepresents “Not-a-Number”, a special value indicating an unrepresentable or undefined numerical result.
Number.NEGATIVE_INFINITYSpecial value representing negative infinity (e.g., result of dividing a negative number by zero).
Number.POSITIVE_INFINITYSpecial value representing positive infinity (e.g., result of dividing a positive number by zero).
Number.prototypeAn object that allows you to add properties and methods to all Number instances. (For advanced use).

These methods are called directly on the Number constructor.

MethodDescription
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)); // true
console.log(Number.isNaN('hello' / 2)); // true
console.log(Number.isFinite(123)); // true
console.log(Number.isFinite(Infinity)); // false
console.log(Number.isInteger(10)); // true
console.log(Number.isInteger(10.5)); // false
console.log(Number.isSafeInteger(Math.pow(2, 53) - 1)); // true
console.log(Number.isSafeInteger(Math.pow(2, 53))); // false

These methods are called on number primitives or Number objects.

MethodDescription
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)

ES6 introduced convenient syntax for binary and octal integer literals. Hexadecimal literals were already available.

  • Binary Literals: Prefix with 0b or 0B. Example: 0b1010 (decimal 10).
  • Octal Literals: Prefix with 0o or 0O. Example: 0o12 (decimal 10). (Note: Old octal literals starting with just 0 are deprecated in strict mode and should be avoided).
  • Hexadecimal Literals: Prefix with 0x or 0X. Example: 0xA (decimal 10).
console.log(0b001); // 1
console.log(0b010); // 2
console.log(0b1111); // 15
console.log(0o010); // 8
console.log(0o100); // 64
console.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)

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 BigInt
const 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 TypeError

BigInts allow for arbitrary-precision integer arithmetic. For more details, see the MDN BigInt documentation.