Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 784719 of Number

  • Revision slug: Web/JavaScript/Reference/Global_Objects/Number
  • Revision title: Number
  • Revision id: 784719
  • Created:
  • Creator: getify
  • Is current revision? No
  • Comment Simplifying descriptions of `isFinite(..)`, `isInteger(..)`, and `isSafeInteger(..)`

Revision Content

{{JSRef("Global_Objects", "Number")}}

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.
{{jsOverrides("Function", "properties", "MAX_VALUE", "MIN_VALUE", "NaN", "NEGATIVE_INFINITY", "POSITIVE_INFINITY", "protoype")}}

Methods

{{js_see_prototype("Number", "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) and 253 - 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.
{{jsOverrides("Function", "Methods", "isNaN")}}

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

{{page('/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/prototype', '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

{{CompatibilityTable}}
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

Revision Source

<div>{{JSRef("Global_Objects", "Number")}}</div>

<h2 id="Summary" name="Summary">Summary</h2>

<p>The <strong><code>Number</code></strong> JavaScript object is a wrapper object allowing you to work with numerical values. A <code>Number</code> object is created using the <code>Number()</code> constructor.</p>

<h2 id="Syntax" name="Syntax">Constructor</h2>

<pre class="syntaxbox">
new Number(value);</pre>

<h3 id="Parameters" name="Parameters">Parameters</h3>

<dl>
 <dt><code>value</code></dt>
 <dd>The numeric value of the object being created.</dd>
</dl>

<h2 id="Description" name="Description">Description</h2>

<p>The primary uses for the <code>Number</code> object are:</p>

<ul>
 <li>If the argument cannot be converted into a number, it returns {{jsxref("Global_Objects/NaN", "NaN")}}.</li>
 <li>In a non-constructor context (i.e., without the {{jsxref("Operators/new", "new")}} operator), <code>Number</code> can be used to perform a type conversion.</li>
</ul>

<h2 id="Properties" name="Properties">Properties</h2>

<dl>
 <dt>{{jsxref("Number.EPSILON")}} {{experimental_inline}}</dt>
 <dd>The smallest interval between two representable numbers.</dd>
 <dt>{{jsxref("Number.MAX_SAFE_INTEGER")}} {{experimental_inline}}</dt>
 <dd>The maximum safe integer in JavaScript (<code>2<sup>53</sup> - 1</code>).</dd>
 <dt>{{jsxref("Number.MAX_VALUE")}}</dt>
 <dd>The largest positive representable number.</dd>
 <dt>{{jsxref("Number.MIN_SAFE_INTEGER")}} {{experimental_inline}}</dt>
 <dd>The minimum safe integer in JavaScript (<code>-(2<sup>53</sup> - 1)</code>).</dd>
 <dt>{{jsxref("Number.MIN_VALUE")}}</dt>
 <dd>The smallest positive representable number - that is, the positive number closest to zero (without actually being zero).</dd>
 <dt>{{jsxref("Number.NaN")}}</dt>
 <dd>Special "not a number" value.</dd>
 <dt>{{jsxref("Number.NEGATIVE_INFINITY")}}</dt>
 <dd>Special value representing negative infinity; returned on overflow.</dd>
 <dt>{{jsxref("Number.POSITIVE_INFINITY")}}</dt>
 <dd>Special value representing infinity; returned on overflow.</dd>
 <dt>{{jsxref("Number.prototype")}}</dt>
 <dd>Allows the addition of properties to a <code>Number</code> object.</dd>
</dl>

<div>{{jsOverrides("Function", "properties", "MAX_VALUE", "MIN_VALUE", "NaN", "NEGATIVE_INFINITY", "POSITIVE_INFINITY", "protoype")}}</div>

<h2 id="Methods" name="Methods">Methods</h2>

<div>{{js_see_prototype("Number", "Methods")}}</div>

<dl>
 <dt>{{jsxref("Number.isNaN()")}} {{experimental_inline}}</dt>
 <dd>Determine whether the passed value is NaN.</dd>
 <dt>{{jsxref("Number.isFinite()")}} {{experimental_inline}}</dt>
 <dd>Determine whether the passed value is a finite number.</dd>
 <dt>{{jsxref("Number.isInteger()")}} {{experimental_inline}}</dt>
 <dd>Determine whether the passed value is an integer.</dd>
 <dt>{{jsxref("Number.isSafeInteger()")}} {{experimental_inline}}</dt>
 <dd>Determine whether the&nbsp;passed value is a safe integer (number between <code>-(2<sup>53</sup> - 1)</code> and <code>2<sup>53</sup> - 1</code>).</dd>
 <dt><s class="obsoleteElement">{{jsxref("Number.toInteger()")}} {{obsolete_inline}}</s></dt>
 <dd><s class="obsoleteElement">Used to evaluate the passed value and convert it to an integer (or {{jsxref("Global_Objects/Infinity", "Infinity")}}), but has been removed.</s></dd>
 <dt>{{jsxref("Number.parseFloat()")}} {{experimental_inline}}</dt>
 <dd>The value is the same as {{jsxref("Global_Objects/parseFloat", "parseFloat")}} of the global object.</dd>
 <dt>{{jsxref("Number.parseInt()")}} {{experimental_inline}}</dt>
 <dd>The value is the same as {{jsxref("Global_Objects/parseInt", "parseInt")}} of the global object.</dd>
</dl>

<div>{{jsOverrides("Function", "Methods", "isNaN")}}</div>

<h2 id="Number_instances" name="Number_instances"><code>Number</code> instances</h2>

<p>All <code>Number</code> instances inherit from {{jsxref("Number.prototype")}}. The prototype object of the <code>Number</code> constructor can be modified to affect all <code>Number</code> instances.</p>

<h3 id="Methods_of_Number_instance" name="Methods_of_Number_instance">Methods</h3>

<div>{{page('/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/prototype', 'Methods')}}</div>

<h2 id="Examples" name="Examples">Examples</h2>

<h3 id="Example:_Using_the_Number_object_to_assign_values_to_numeric_variables" name="Example:_Using_the_Number_object_to_assign_values_to_numeric_variables">Example: Using the <code>Number</code> object to assign values to numeric variables</h3>

<p>The following example uses the <code>Number</code> object's properties to assign values to several numeric variables:</p>

<pre class="brush: js">
var biggestNum = Number.MAX_VALUE;
var smallestNum = Number.MIN_VALUE;
var infiniteNum = Number.POSITIVE_INFINITY;
var negInfiniteNum = Number.NEGATIVE_INFINITY;
var notANum = Number.NaN;
</pre>

<h3 id="Example:_Using_Number_to_convert_a_Date_object" name="Example:_Using_Number_to_convert_a_Date_object">Example: Integer range for <code>Number</code></h3>

<p>The following example shows minimum and maximum integer values that can be represented as <code>Number</code> object (for details, refer to EcmaScript standard, chapter <em>8.5 The Number Type</em>):</p>

<pre class="brush: js">
var biggestInt = 9007199254740992;
var smallestInt = -9007199254740992;
</pre>

<p>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 <code>Number</code> type. Using {{jsxref("Global_Objects/String", "String")}} instead is a possible workaround.</p>

<h3 id="Example:_Using_Number_to_convert_a_Date_object" name="Example:_Using_Number_to_convert_a_Date_object">Example: Using <code>Number</code> to convert a <code>Date</code> object</h3>

<p>The following example converts the {{jsxref("Global_Objects/Date", "Date")}} object to a numerical value using <code>Number</code> as a function:</p>

<pre class="brush: js">
var d = new Date('December 17, 1995 03:24:00');
print(Number(d));
</pre>

<p>This displays "819199440000".</p>

<h3 id="Example.3A_Convert_numeric_strings_to_numbers">Example: Convert numeric strings to numbers</h3>

<pre class="brush: js">
Number("123")     // 123
Number("")        // 0
Number("0x11")    // 17
Number("0b11")    // 3
Number("0o11")    // 9
Number("foo")     // NaN
Number("100a")    // NaN
</pre>

<h2 id="Specifications">Specifications</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>ECMAScript 1st Edition.</td>
   <td>Standard</td>
   <td>Initial definition. Implemented in JavaScript 1.1.</td>
  </tr>
  <tr>
   <td>{{SpecName('ES5.1', '#sec-15.7', 'Number')}}</td>
   <td>{{Spec2('ES5.1')}}</td>
   <td>&nbsp;</td>
  </tr>
  <tr>
   <td>{{SpecName('ES6', '#sec-number-objects', 'Number')}}</td>
   <td>{{Spec2('ES6')}}</td>
   <td>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")}})</td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility">Browser compatibility</h2>

<div>{{CompatibilityTable}}</div>

<div id="compat-desktop">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Chrome</th>
   <th>Firefox (Gecko)</th>
   <th>Internet Explorer</th>
   <th>Opera</th>
   <th>Safari</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
  </tr>
 </tbody>
</table>
</div>

<div id="compat-mobile">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Android</th>
   <th>Chrome for Android</th>
   <th>Firefox Mobile (Gecko)</th>
   <th>IE Mobile</th>
   <th>Opera Mobile</th>
   <th>Safari Mobile</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
  </tr>
 </tbody>
</table>
</div>

<h2 id="See_also" name="See_also">See also</h2>

<ul>
 <li>{{jsxref("Global_Objects/NaN", "NaN")}}</li>
 <li>The {{jsxref("Global_Objects/Math", "Math")}} global object</li>
</ul>
Revert to this revision