Array.length

  • Revision slug: Web/JavaScript/Reference/Global_Objects/Array/length
  • Revision title: Array.length
  • Revision id: 417563
  • Created:
  • Creator: Sheppy
  • Is current revision? No
  • Comment Moved From JavaScript/Reference/Global_Objects/Array/length to Web/JavaScript/Reference/Global_Objects/Array/length

Revision Content

Summary

An unsigned, 32-bit integer that specifies the number of elements in an array.

Property of Array
Implemented in JavaScript 1.1
ECMAScript Edition ECMAScript 1st Edition

Syntax

array.length

Description

The value of the length property is an integer with a positive sign and a value less than 2 to the 32 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.

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. Each value 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
}

Revision Source

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

<p>An unsigned, 32-bit integer that specifies the number of elements in an array.</p>
<table class="standard-table">
  <thead>
    <tr>
      <th class="header" colspan="2">Property of <a href="/en-US/docs/JavaScript/Reference/Global_Objects/Array" title="JavaScript/Reference/Global_Objects/Array"><code>Array</code></a></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Implemented in</td>
      <td>JavaScript 1.1</td>
    </tr>
    <tr>
      <td>ECMAScript Edition</td>
      <td>ECMAScript 1st Edition</td>
    </tr>
  </tbody>
</table>

<h2 id="Syntax" name="Syntax">Syntax</h2>
<pre class="syntaxbox"><var>array</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 32 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.</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. Each value 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>


<!--languages({ "fr": "fr/R\u00e9f\u00e9rence_de_JavaScript_1.5_Core/Objets_globaux/Array/length" })-->
Revert to this revision