mozilla

Revision 503529 of Array.prototype.length

  • Revision slug: Web/JavaScript/Reference/Global_Objects/Array/length
  • Revision title: Array.length
  • Revision id: 503529
  • Created:
  • Creator: fscholz
  • Is current revision? No
  • Comment

Revision Content

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

Summary

The length property represents an unsigned, 32-bit integer that specifies the number of elements in an array.

{{js_property_attributes(1,0,0)}}

Syntax

arr.length

Description

The value of the length property is an integer with a positive sign and a value less than 2 to the 32nd power (232).

You can set the length property to truncate an array at any time. When you extend an array by changing its length property, the number of actual elements does not increase; for example, if you set length to 3 when it is currently 2, the array still contains only 2 elements.  Thus, the length property says nothing about the number of defined values in the array. See also Relationship between length and numerical properties.

Examples

Example: Iterating over an array

In the following example the array numbers is iterated through by looking at the length property to see how many elements it has. The value in each element is then doubled.

var numbers = [1,2,3,4,5];

for (var i = 0; i < numbers.length; i++) {
  numbers[i] *= 2;
}
// numbers is now [2,4,6,8,10];

Example: Shortening an array

The following example shortens the array statesUS to a length of 50 if the current length is greater than 50.

if (statesUS.length > 50) {
   statesUS.length=50
}

Specifications

Specification Status Comment
ECMAScript 1st Edition. Standard Initial definition.
{{SpecName('ES5.1', '#sec-15.4.5.2', 'Array.length')}} {{Spec2('ES5.1')}}  
{{SpecName('ES6', '#sec-properties-of-array-instances-length', 'Array.length')}} {{Spec2('ES6')}}  

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/Array", "Array")}}

Revision Source

<div>
 {{JSRef("Global_Objects", "Array")}}</div>
<h2 id="Summary" name="Summary">Summary</h2>
<p>The <code><strong>length</strong></code> property represents an unsigned, 32-bit integer that specifies the number of elements in an array.</p>
<div>
 {{js_property_attributes(1,0,0)}}</div>
<h2 id="Syntax" name="Syntax">Syntax</h2>
<pre class="syntaxbox">
<var>arr</var>.length</pre>
<h2 id="Description" name="Description">Description</h2>
<p>The value of the <code>length</code> property is an integer with a positive sign and a value less than 2 to the 32nd power (2<sup>32</sup>).</p>
<p>You can set the <code>length</code> property to truncate an array at any time. When you extend an array by changing its <code>length</code> property, the number of actual elements does not increase; for example, if you set <code>length</code> to 3 when it is currently 2, the array still contains only 2 elements. &nbsp;<span style="line-height: 1.572;">Thus, the&nbsp;</span><code style="font-size: 14px;">length</code><span style="line-height: 1.572;">&nbsp;property says nothing about the number of defined values in the array.&nbsp;</span><span style="line-height: 1.572;">See also&nbsp;</span><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array#Relationship_between_length_and_numerical_properties" style="line-height: 1.572;" title="Relationship between length and numerical properties">Relationship between length and numerical properties</a><span style="line-height: 1.572;">.</span></p>
<h2 id="Examples" name="Examples">Examples</h2>
<h3 id="Example:_Iterating_over_an_array" name="Example:_Iterating_over_an_array">Example: Iterating over an array</h3>
<p>In the following example the array <code>numbers</code> is iterated through by looking at the <code>length</code> property to see how many elements it has. The value in each element is then doubled.</p>
<pre class="brush:js">
var numbers = [1,2,3,4,5];

for (var i = 0; i &lt; numbers.length; i++) {
  numbers[i] *= 2;
}
// numbers is now [2,4,6,8,10];
</pre>
<h3 id="Example:_Shortening_an_array" name="Example:_Shortening_an_array">Example: Shortening an array</h3>
<p>The following example shortens the array <code>statesUS</code> to a length of 50 if the current length is greater than 50.</p>
<pre class="brush:js">
if (statesUS.length &gt; 50) {
   statesUS.length=50
}</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.</td>
  </tr>
  <tr>
   <td>{{SpecName('ES5.1', '#sec-15.4.5.2', 'Array.length')}}</td>
   <td>{{Spec2('ES5.1')}}</td>
   <td>&nbsp;</td>
  </tr>
  <tr>
   <td>{{SpecName('ES6', '#sec-properties-of-array-instances-length', 'Array.length')}}</td>
   <td>{{Spec2('ES6')}}</td>
   <td>&nbsp;</td>
  </tr>
 </tbody>
</table>
<h2 id="Browser_compatibility">Browser compatibility</h2>
<p>{{ CompatibilityTable() }}</p>
<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/Array", "Array")}}</li>
</ul>
<!--languages({ "fr": "fr/R\u00e9f\u00e9rence_de_JavaScript_1.5_Core/Objets_globaux/Array/length" })-->
Revert to this revision