La proprieta' length setta o restituisce il numero di elementi in un array. E' un intero a 32 bit, sempre maggiore del piu' grande indice dell'array.
var items = ['shoes', 'shirts', 'socks', 'sweaters'];
items.length;
// returns 4
Descrizione
Puoi impostare la proprietà length di un array per troncare o estendere l'array stesso.
var a = [1,2,3];
a.length=2;
// a is now [1, 2]
a.length = 10
// a is now [1, 2, undefined × 8]
Esempi
Iterare su un array
Nel seguente esempio, l'array numbers viene iterarato e ciascun elemento viene moltiplicato per 2.
var numbers = [1, 2, 3, 4, 5];
var length = numbers.length;
for (var i = 0; i < length; i++) {
numbers[i] *= 2;
}
// numbers is now [2, 4, 6, 8, 10]
Specifiche
Specifiche | Stato | Commento |
---|---|---|
ECMAScript 1st Edition (ECMA-262) | Standard | Definizione iniziale. |
ECMAScript 5.1 (ECMA-262) The definition of 'Array.length' in that specification. |
Standard | |
ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'Array.length' in that specification. |
Standard | |
ECMAScript Latest Draft (ECMA-262) The definition of 'Array.length' in that specification. |
Draft |
Compatibilità
We're converting our compatibility data into a machine-readable JSON format.
This compatibility table still uses the old format,
because we haven't yet converted the data it contains.
Find out how you can help!
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|
Basic support | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
Feature | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|
Basic support | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |