1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
|
---
title: Number
slug: Web/JavaScript/Reference/Global_Objects/Number
tags:
- JavaScript
- JavaScript Reference
- Number
- Reference
- Référence(2)
translation_of: Web/JavaScript/Reference/Global_Objects/Number
---
<div>{{JSRef}}</div>
<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>
<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 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_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> </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>
|