/

JavaScript Reference: Number Properties and Methods

JavaScript Reference: Number Properties and Methods

In this article, we will explore the properties and methods of the JavaScript Number built-in object.

A number value can be created using a number literal syntax, like this:

1
2
const age = 36
typeof age // "number"

Alternatively, you can use the Number global function to generate a number value:

1
2
const age = Number(36)
typeof age // "number"

When using the new keyword with the Number function, an instance of the Number object is returned instead:

1
2
const age = new Number(36)
typeof age // "object"

The Number object behaves differently from a primitive number type. To obtain the original number value from the Number object, you can use the valueOf() method:

1
2
3
const age = new Number(36)
typeof age // "object"
age.valueOf() // 36

Properties:

  • EPSILON: the smallest interval between two numbers
  • MAX_SAFE_INTEGER: the maximum integer value JavaScript can represent
  • MAX_VALUE: the maximum positive value JavaScript can represent
  • MIN_SAFE_INTEGER: the minimum integer value JavaScript can represent
  • MIN_VALUE: the minimum positive value JavaScript can represent
  • NaN: a special value representing “not a number”
  • NEGATIVE_INFINITY: a special value representing negative infinity
  • POSITIVE_INFINITY: a special value representing positive infinity

The above properties evaluate to the following values:

1
2
3
4
5
6
7
8
Number.EPSILON // 2.220446049250313e-16
Number.MAX_SAFE_INTEGER // 9007199254740991
Number.MAX_VALUE // 1.7976931348623157e+308
Number.MIN_SAFE_INTEGER // -9007199254740991
Number.MIN_VALUE // 5e-324
Number.NaN // NaN
Number.NEGATIVE_INFINITY // -Infinity
Number.POSITIVE_INFINITY // Infinity

Object Methods:

You can use the following methods with the Number object by passing a value as an argument:

  • Number.isNaN(value): returns true if value is not a number
  • Number.isFinite(value): returns true if value is a finite number
  • Number.isInteger(value): returns true if value is an integer
  • Number.isSafeInteger(value): returns true if value is a safe integer
  • Number.parseFloat(value): converts value to a floating-point number and returns it
  • Number.parseInt(value): converts value to an integer and returns it

The term “safe integer” refers to an integer that can be exactly represented as an IEEE-754 double precision number, within the range of (2^53 - 1) to -(2^53 - 1). Integers outside this range cannot be represented correctly by JavaScript. You can learn more about safe integers in this detailed explanation.

Instance Methods:

When using the new keyword to create a Number object, you can access the following unique instance methods:

  • .toExponential(): returns a string representing the number in exponential notation
  • .toFixed(): returns a string representing the number in fixed-point notation
  • .toLocaleString(): returns a string with the number formatted according to local conventions
  • .toPrecision(): returns a string representing the number with a specified precision
  • .toString(): returns a string representing the object in a specified radix (base). Overrides the Object.prototype.toString() method
  • .valueOf(): returns the number primitive value of the object

Tags: JavaScript Number, Number properties, Number methods