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 1129669 of array.length

  • Revision slug: Web/JavaScript/Reference/Global_Objects/Array/length
  • Revision title: array.length
  • Revision id: 1129669
  • Created:
  • Creator: fscholz
  • Is current revision? Yes
  • Comment add simple example, add simple intro sentence

Revision Content

{{JSRef}}

The array length property sets or returns the number of elements in an array. It represents an unsigned, 32-bit integer that is always numerically greater than the highest index in the 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 does not necessarily indicate the number of defined values in the array. See also Relationship between length and numerical properties.

Examples

Returning the length of an array

var items = ["shoes", "shirts", "socks", "sweaters"];

items.length; 

// returns 4

Iterating over an array

In the following example, the array numbers is iterated through by looking at the length property. 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]

Shortening an array

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

if (items.length > 50) {
  items.length = 50;
}

Specifications

Specification Status Comment
{{SpecName('ES1')}} {{Spec2('ES1')}} 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')}}  
{{SpecName('ESDraft', '#sec-properties-of-array-instances-length', 'Array.length')}} {{Spec2('ESDraft')}}  

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

Revision Source

<div>{{JSRef}}</div>

<p>The array <code><strong>length</strong></code> property sets or returns the number of elements in an array. It represents an unsigned, 32-bit integer that&nbsp;is always numerically greater than the highest index in the array.</p>

<div>{{js_property_attributes(1, 0, 0)}}</div>

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

<pre class="syntaxbox">
<var>arr</var>.length</pre>

<h2 id="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. Thus, the <code>length</code> property&nbsp;does not necessarily indicate&nbsp;the number of defined values in the array. See also <a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array#Relationship_between_length_and_numerical_properties" title="Relationship between length and numerical properties">Relationship between <code>length</code> and numerical properties</a>.</p>

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

<h3>Returning the length of an array</h3>

<pre class="brush: js">
var items = ["shoes", "shirts", "socks", "sweaters"];

items.length; 

// returns 4</pre>

<h3 id="Iterating_over_an_array">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. 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="Shortening_an_array">Shortening an array</h3>

<p>The following example shortens the array <code>items</code> to a length of 50 if the current length is greater than 50.</p>

<pre class="brush: js">
if (items.length &gt; 50) {
  items.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>{{SpecName('ES1')}}</td>
   <td>{{Spec2('ES1')}}</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>
  <tr>
   <td>{{SpecName('ESDraft', '#sec-properties-of-array-instances-length', 'Array.length')}}</td>
   <td>{{Spec2('ESDraft')}}</td>
   <td>&nbsp;</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">See also</h2>

<ul>
 <li>{{jsxref("Array")}}</li>
</ul>
Revert to this revision