Javascript array length always 1

The array in JavaScript is a simple zero-based structure. The array.length returns the n + 1 where n is the maximum index in an array.

That's just how it works - when you assign 90'th element and this array's length is less than 90, it expands an array to 90 and sets the 90-th element's value. All missing values are interpreted as null.

If you try the following code:

var a = [];
a[21] = {};
a[90] = {};
a[13] = {};
console.log[JSON.stringify[a]];

You will get the following JSON:

[null,null,null,null,null,null,null,null,null,null,null,null,null,{},null,null,null,null,null,null,null,{},null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,{}]

Moreover, array.length is not a readonly value.
If you set a length value less than the current, then the array will be resized:

 var arr = [1,2,3,4,5];
 arr.length = 3;
 console.log[JSON.stringify[arr]];
 // [1,2,3]

If you set a length value more than the current, then the array will be expanded as well:

 var arr = [1,2,3];
 arr.length = 5;
 console.log[JSON.stringify[arr]];
 // [1,2,3,null,null]

In case you need to assign such values, you can use JS objects.
You can use them as associative array and assign any key-value pairs.

var a = {};
a[21] = 'a';
a[90] = 'b';
a[13] = 'c';
a['stringkey'] = 'd';
a.stringparam = 'e'; // btw, a['stringkey'] and a.stringkey is the same

console.log[JSON.stringify[a]]; 
// returns {"13":"c","21":"a","90":"b","stringkey":"d","stringparam":"e"}

console.log[Object.keys[a].length];
// returns 5

The length property of an object which is an instance of type Array sets or returns the number of elements in that array. The value is an unsigned, 32-bit integer that is always numerically greater than the highest index in the array.

Try it

Description

The value of the length property is an integer with a positive sign and a value less than 2 to the 32nd power [2^32].

const listA = [1,2,3];
const listB = new Array[6];

console.log[listA.length];
// 3

console.log[listB.length];
// 6

listB.length = 4294967296; //2 to the 32nd power = 4294967296
// RangeError: Invalid array length

const listC = new Array[-100] //negative sign
// RangeError: Invalid array length

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 increases; for example, if you set length to 3 when it is currently 2, the array now contains 3 elements, which causes the third element to be a non-iterable empty slot.

const arr = [1, 2];
console.log[arr];
// [ 1, 2 ]

arr.length = 5; // set array length to 5 while currently 2.
console.log[arr];
// [ 1, 2,  ]

arr.forEach[[element] => console.log[element]];
// 1
// 2

As you can see, the length property does not necessarily indicate the number of defined values in the array. See also Relationship between length and numerical properties.

  • Writable: If this attribute set to false, the value of the property cannot be changed.
  • Configurable: If this attribute set to false, any attempts to delete the property or change its attributes [Writable, Configurable, or Enumerable] will fail.
  • Enumerable: If this attribute set to true, the property will be iterated over during for or for...in loops.

Examples

Iterating over an array

In the following example, the array numbers is iterated through by looking at the length property. The value in each element is then doubled.

const numbers = [1, 2, 3, 4, 5];
const length = numbers.length;
for [let i = 0; i  3] {
  numbers.length = 3;
}

console.log[numbers]; // [1, 2, 3]
console.log[numbers.length]; // 3

Create empty array of fixed length

const numbers = [];
numbers.length = 3;
console.log[numbers]; // [empty x 3]

Specifications

Specification
ECMAScript Language Specification
# sec-properties-of-array-instances-length

Browser compatibility

BCD tables only load in the browser

See also

Does array length start 0 or 1?

Arrays in Java use zero-based counting. This means that the first element in an array is at index zero. However, the Java array length does not start counting at zero.

What does array length

length -1 means, specifically the -1 part. When using a for loop over an array we have something like this: for [i = 0; i < array.

Do arrays start at 0 or 1 in JavaScript?

JavaScript arrays are zero-indexed: the first element of an array is at index 0 , the second is at index 1 , and so on — and the last element is at the value of the array's length property minus 1 .

Why do we subtract 1 from array length in JavaScript?

The reason we are subtracting 1 from the length is, in JavaScript, the array index numbering starts with 0. i.e. 1st element's index would 0. Therefore the last element's index would be array length-1.

Chủ Đề