Apply your JS skills to key Mozilla projects as an MDN Fellow! http://mzl.la/MDNFellowship

mozilla

Revision 483049 of Array.length

  • Revision slug: Web/JavaScript/Reference/Global_Objects/Array/length
  • Revision title: Array.length
  • Revision id: 483049
  • Created:
  • Creator: michaelficarra
  • Is current revision? No
  • Comment Reverting a recent revision by LouisLazaris (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/length%24compare?to=477583&from=455779) which was factually inaccurate.

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

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