Array.length

  • Revision slug: JavaScript/Reference/Global_Objects/Array/length
  • Revision title: length
  • Revision id: 82128
  • Created:
  • Creator: Raevel
  • Is current revision? No
  • Comment /* Example: Iterating over an array */

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, NES 2.0

JavaScript 1.3: length is an unsigned, 32-bit integer with a value less than 232.

ECMA Version: ECMA-262

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, and each value is doubled.

var numbers = [1,2,3,4,5];
for (var i = 0, i < numbers.length; i++) {
  numbers[i] *= 2; // numbers[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

<p>
</p>
<h3 name="Summary"> Summary </h3>
<p>An unsigned, 32-bit integer that specifies the number of elements in an array.
</p>
<table class="fullwidth-table">
<tbody><tr>
<td class="header" colspan="2">Property of <a href="en/Core_JavaScript_1.5_Reference/Objects/Array">Array</a></td>
</tr>
<tr>
<td>Implemented in:</td>
<td>JavaScript 1.1, NES 2.0
<p>JavaScript 1.3: <code>length</code> is an unsigned, 32-bit integer with a value less than 2<sup>32</sup>.
</p>
</td>
</tr>
<tr>
<td>ECMA Version:</td>
<td>ECMA-262</td>
</tr>
</tbody></table>
<h3 name="Description"> Description </h3>
<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>
<h3 name="Examples"> Examples </h3>
<h4 name="Example:_Iterating_over_an_array"> Example: Iterating over an array </h4>
<p>In the following example the Array <code>numbers</code> is iterated through, and each value is doubled.
</p>
<pre>var numbers = [1,2,3,4,5];
for (var i = 0, i &lt; numbers.length; i++) {
  numbers[i] *= 2; // numbers[i] = numbers[i] * 2;
}
// numbers is now [2,4,6,8,10];
</pre>
<h4 name="Example:_Shortening_an_array"> Example: Shortening an array </h4>
<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>if (statesUS.length &gt; 50) {
   statesUS.length=50
}
</pre>
Revert to this revision