mozilla

Revision 82127 of Array.prototype.length

  • Revision slug: JavaScript/Reference/Global_Objects/Array/length
  • Revision title: length
  • Revision id: 82127
  • Created:
  • Creator: Maian
  • Is current revision? No
  • Comment Core JavaScript 1.5 Reference:Objects:Array:length moved to Core JavaScript 1.5 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, 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 getChoice function uses the length property to iterate over every element in the musicType array. musicType is a select element on the musicForm form.

function getChoice() {
   for (var i = 0; i < document.musicForm.musicType.length; i++) {
      if (document.musicForm.musicType.options[i].selected == true) {
         return document.musicForm.musicType.options[i].text
      }
   }
}

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 <code>getChoice</code> function uses the <code>length</code> property to iterate over every element in the <code>musicType</code> array. <code>musicType</code> is a select element on the <code>musicForm</code> form.
</p>
<pre>function getChoice() {
   for (var i = 0; i &lt; document.musicForm.musicType.length; i++) {
      if (document.musicForm.musicType.options[i].selected == true) {
         return document.musicForm.musicType.options[i].text
      }
   }
}
</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