JavaScript - The Number Object
JavaScript - The Number Object
The JavaScript Number object represents numerical data as floating-point numbers. It contains different properties
(constants) and methods for working on numbers. In general, you do not need to worry about Number objects because
the browser automatically converts number literals to instances of the number class.
A JavaScript Number object can be defined using the Number constructor. Other types of data such as strings, etc., can be
converted to numbers using Number() function.
A JavaScript number is always stored as a floating-point value (decimal number). JavaScript does not make a distinction
between integer values and floating-point values. JavaScript represents numbers using the 64-bit floating-point format
defined by the IEEE 754 standard.
Syntax
The syntax for creating a number object is as follows −
In the place of number, if you provide any non-number argument, then the argument cannot be converted into a number,
it returns NaN (Not-a-Number).
We can also create the number primitives by assigning the numeric values to the variables −
The JavaScript automatically converts the number primitive to the Number objects. So we can use all properties and
methods of Number object on number primitives.
Number Properties
Here is a list of each property and their description.
EPSILON
1
It represents the difference between 1 and the smallest floating point number greater than 1.
2 MAX_SAFE_INTEGER
It returns the maximum safe integer value.
MAX_VALUE
3
The largest possible value a number in JavaScript can have 1.7976931348623157E+308.
MIN_SAFE_INTEGER
4
It returns the minimum safe integer value.
MIN_VALUE
5
The smallest possible value a number in JavaScript can have 5E-324.
NaN
6
Equal to a value that is not a number.
NEGATIVE_INFINITY
7
A value that is less than MIN_VALUE.
POSITIVE_INFINITY
8
A value that is greater than MAX_VALUE
prototype
9 A static property of the Number object. Use the prototype property to assign new properties and methods to
the Number object in the current document.
constructor
10
Returns the function that created this object's instance. By default this is the Number object.
Number Methods
The Number object contains only the default methods (instance and static methods) that are a part of every object's
definition.
Instance Methods
toExponential()
1
Forces a number to display in exponential notation, even if the number is in the range of standard notation.
toFixed()
2
Formats a number with a specific number of digits to the right of the decimal.
toLocaleString()
3
Returns a string value version of the current number in a format that may vary according to local settings.
toPrecision()
4
Defines how many total digits (including digits to the left and right of the decimal) to display of a number.
toString()
5
Returns the string representation of the number's value.
valueOf()
6
Returns the number's value.
Static Methods
isNaN()
1
It checks whether the value is a valid number or not.
isFinite()
2
It checks whether the number is finite.
isInteger()
3
Returns Boolean when the number is an integer value.
isSafeInteger()
4
It ensures that the integer is a safe integer.
parseFloat()
5
Parses the float value from the string.
parseInt()
6
Parses the integer value from the string.
Examples
Let's take a few examples to demonstrate the properties and methods of Number.
In the example below, the num variable contains the number object having the value 20. In the output, you can see that
type of the num variable is an object.
<html>
<body>
<p id = "output"> </p>