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:
const age = 36
typeof age // "number"
Alternatively, you can use the Number
global function to generate a number value:
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:
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:
const age = new Number(36)
typeof age // "object"
age.valueOf() // 36
Properties:
EPSILON
: the smallest interval between two numbersMAX_SAFE_INTEGER
: the maximum integer value JavaScript can representMAX_VALUE
: the maximum positive value JavaScript can representMIN_SAFE_INTEGER
: the minimum integer value JavaScript can representMIN_VALUE
: the minimum positive value JavaScript can representNaN
: a special value representing “not a number”NEGATIVE_INFINITY
: a special value representing negative infinityPOSITIVE_INFINITY
: a special value representing positive infinity
The above properties evaluate to the following values:
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 ifvalue
is not a numberNumber.isFinite(value)
: returns true ifvalue
is a finite numberNumber.isInteger(value)
: returns true ifvalue
is an integerNumber.isSafeInteger(value)
: returns true ifvalue
is a safe integerNumber.parseFloat(value)
: convertsvalue
to a floating-point number and returns itNumber.parseInt(value)
: convertsvalue
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 theObject.prototype.toString()
method.valueOf()
: returns the number primitive value of the object
Tags: JavaScript Number, Number properties, Number methods