| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/es/docs/Web/JavaScript/Reference/Global_Objects/Array/length | [Back] [Original] |
Get to know MDN better
Esta pgina ha sido traducida del ingls por la comunidad. Aprende ms y nete a la comunidad de MDN Web Docs.
This feature is well established and works across many devices and browser versions. Its been available across browsers since julio de 2015.
La propiedad length de un objeto que es una instancia de tipo Array establece o devuelve la cantidad de elementos en esa matriz. El valor es un entero sin signo de 32 bits que siempre es numricamente mayor que el ndice ms alto en la matriz.
const clothing = ["shoes", "shirts", "socks", "sweaters"];
console.log(clothing.length);
// Expected output: 4
El valor de la propiedad length es un nmero entero con un signo positivo y un valor menor que 2 a la 32a potencia (232).
var namelistA = new Array(4294967296); //2 a la 32a potencia = 4294967296
var namelistC = new Array(-100); //signo negativo
console.log(namelistA.length); //RangeError: longitud de la matriz invlida
console.log(namelistC.length); //RangeError: longitud de la matriz invlida
var namelistB = [];
namelistB.length = Math.pow(2, 32) - 1; //establecer una longitud de la matriz menor que 2 a la 32 potencia
console.log(namelistB.length);
//4294967295
Puedes establecer la propiedad length para truncar una matriz unidimensional en cualquier momento. Cuando extiende una matriz cambiando su propiedad length, el nmero de elementos reales aumenta; por ejemplo, si se establece length en 3 cuando actualmente es 2, la matriz ahora contiene 3 elementos, lo que hace que el tercer elemento sea undefined.
var arr = [1, 2, 3];
printEntries(arr);
arr.length = 5; // establecer la longitud de la matriz en 5 mientras que actualmente es 3.
printEntries(arr);
function printEntries(arr) {
var length = arr.length;
for (var i = 0; i < length; i++) {
console.log(arr[i]);
}
console.log("=== printed ===");
}
// 1
// 2
// 3
// === impreso ===
// 1
// 2
// 3
// undefined
// undefined
// === impreso ===
Pero, la propiedad length no necesariamente indica el nmero de valores definidos en la matriz. Ver tambin Relacin entre length y las propiedades numricas.
Atributos de la propiedad Array.prototype.length | |
|---|---|
| Sobrescribir | S |
| Numerable | No |
| Configurable | No |
Sobrescribir: si este atributo se establece en false, el valor de la propiedad no se puede cambiar.Configurable: si este atributo se establece en false, cualquier intento de eliminar la propiedad o cambiar sus atributos (Sobrescribir, Configurable o Numerable) fallar.Numerable: si este atributo se establece en true, la propiedad se repetir durante los bucles for o for..in.En el siguiente ejemplo, la matriz numbers se itera a travs de la propiedad length. El valor en cada elemento se duplica.
var numbers = [1, 2, 3, 4, 5];
var length = numbers.length;
for (var i = 0; i < length; i++) {
numbers[i] *= 2;
}
// numbers ahora es [2, 4, 6, 8, 10]
El siguiente ejemplo acorta los numbers de la matriz a una longitud de 3 si la longitud actual es mayor que 3.
var numbers = [1, 2, 3, 4, 5];
if (numbers.length > 3) {
numbers.length = 3;
}
console.log(numbers); // [1, 2, 3]
console.log(numbers.length); // 3
| Specification |
|---|
| ECMAScript 2027 LanguageSpecification # sec-properties-of-array-instances-length |
This page was last modified on 11 feb 2025 by MDN contributors.
ArrayArray.prototype.at()Array.prototype.concat()Array.prototype.copyWithin()Array.prototype.entries()Array.prototype.every()Array.prototype.fill()Array.prototype.filter()Array.prototype.find()Array.prototype.findIndex()findLast()findLastIndex()Array.prototype.flat()Array.prototype.flatMap()Array.prototype.forEach()Array.prototype.includes()Array.prototype.indexOf()Array.prototype.join()Array.prototype.keys()Array.prototype.lastIndexOf()Array.prototype.map()Array.prototype.pop()Array.prototype.push()Array.prototype.reduce()Array.prototype.reduceRight()Array.prototype.reverse()Array.prototype.shift()Array.prototype.slice()Array.prototype.some()Array.prototype.sort()Array.prototype.splice()Array.prototype.toLocaleString()toReversed()Array.prototype.toSorted()toSpliced()Array.prototype.toString()Array.prototype.unshift()Array.prototype.values()with()Array.prototype[@@iterator]()Object/FunctionYour blueprint for a better internet.
Portions of this content are 19982026 by individual mozilla.org contributors. Content available under a Creative Commons license.
| Web Proxy Viewer | New URL | Original Page |