Summary
The Number
JavaScript object is a wrapper object allowing you to work with numerical values. A Number
object is created using the Number()
constructor.
Constructor
new Number(value);
Parameters
value
- The numeric value of the object being created.
Description
The primary uses for the Number
object are:
- If the argument cannot be converted into a number, it returns {{jsxref("Global_Objects/NaN", "NaN")}}.
- In a non-constructor context (i.e., without the {{jsxref("Operators/new", "new")}} operator),
Number
can be used to perform a type conversion.
Properties
- {{jsxref("Number.EPSILON")}} {{experimental_inline}}
- The smallest interval between two representable numbers.
- {{jsxref("Number.MAX_SAFE_INTEGER")}} {{experimental_inline}}
- The maximum safe integer in JavaScript (
253 - 1
). - {{jsxref("Number.MAX_VALUE")}}
- The largest positive representable number.
- {{jsxref("Number.MIN_SAFE_INTEGER")}} {{experimental_inline}}
- The minimum safe integer in JavaScript (
-(253 - 1)
). - {{jsxref("Number.MIN_VALUE")}}
- The smallest positive representable number - that is, the positive number closest to zero (without actually being zero).
- {{jsxref("Number.NaN")}}
- Special "not a number" value.
- {{jsxref("Number.NEGATIVE_INFINITY")}}
- Special value representing negative infinity; returned on overflow.
- {{jsxref("Number.POSITIVE_INFINITY")}}
- Special value representing infinity; returned on overflow.
- {{jsxref("Number.prototype")}}
- Allows the addition of properties to a
Number
object.
Methods
- {{jsxref("Number.isNaN()")}} {{experimental_inline}}
- Determine whether the passed value is NaN.
- {{jsxref("Number.isFinite()")}} {{experimental_inline}}
- Determine whether the passed value is a finite number.
- {{jsxref("Number.isInteger()")}} {{experimental_inline}}
- Determine whether the passed value is an integer.
- {{jsxref("Number.isSafeInteger()")}} {{experimental_inline}}
- Determine whether the passed value is a safe integer (number between
-(253 - 1)
and253 - 1
). {{jsxref("Number.toInteger()")}} {{obsolete_inline}}Used to evaluate the passed value and convert it to an integer (or {{jsxref("Global_Objects/Infinity", "Infinity")}}), but has been removed.- {{jsxref("Number.parseFloat()")}} {{experimental_inline}}
- The value is the same as {{jsxref("Global_Objects/parseFloat", "parseFloat")}} of the global object.
- {{jsxref("Number.parseInt()")}} {{experimental_inline}}
- The value is the same as {{jsxref("Global_Objects/parseInt", "parseInt")}} of the global object.
Number
instances
All Number
instances inherit from {{jsxref("Number.prototype")}}. The prototype object of the Number
constructor can be modified to affect all Number
instances.
Methods
Examples
Example: Using the Number
object to assign values to numeric variables
The following example uses the Number
object's properties to assign values to several numeric variables:
var biggestNum = Number.MAX_VALUE; var smallestNum = Number.MIN_VALUE; var infiniteNum = Number.POSITIVE_INFINITY; var negInfiniteNum = Number.NEGATIVE_INFINITY; var notANum = Number.NaN;
Example: Integer range for Number
The following example shows minimum and maximum integer values that can be represented as Number
object (for details, refer to EcmaScript standard, chapter 8.5 The Number Type):
var biggestInt = 9007199254740992; var smallestInt = -9007199254740992;
When parsing data that has been serialized to JSON, integer values falling out of this range can be expected to become corrupted when JSON parser coerces them to Number
type. Using {{jsxref("Global_Objects/String", "String")}} instead is a possible workaround.
Example: Using Number
to convert a Date
object
The following example converts the {{jsxref("Global_Objects/Date", "Date")}} object to a numerical value using Number
as a function:
var d = new Date('December 17, 1995 03:24:00'); print(Number(d));
This displays "819199440000".
Example: Convert numeric strings to numbers
Number("123") // 123 Number("") // 0 Number("0x11") // 17 Number("0b11") // 3 Number("0o11") // 9 Number("foo") // NaN Number("100a") // NaN
Specifications
Specification | Status | Comment |
---|---|---|
ECMAScript 1st Edition. | Standard | Initial definition. Implemented in JavaScript 1.1. |
{{SpecName('ES5.1', '#sec-15.7', 'Number')}} | {{Spec2('ES5.1')}} | |
{{SpecName('ES6', '#sec-number-objects', 'Number')}} | {{Spec2('ES6')}} | New methods and properties added: ({{jsxref("Number.EPSILON", "EPSILON")}}, {{jsxref("Number.isFinite", "isFinite")}}, {{jsxref("Number.isInteger", "isInteger")}}, {{jsxref("Number.isNaN", "isNaN")}}, {{jsxref("Number.parseFloat", "parseFloat")}}, {{jsxref("Number.parseInt", "parseInt")}}) |
Browser compatibility
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|
Basic support | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} |
Feature | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|
Basic support | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} |
See also
- {{jsxref("Global_Objects/NaN", "NaN")}}
- The {{jsxref("Global_Objects/Math", "Math")}} global object