Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding an element in negative index of an array?

Tags:

javascript

arr = Array(10).fill(false)
arr[-2] = true

console.log(arr)
[false, false, false, false, false, false, false, false, false, false, -2: true]

console.log(arr.length) // 10

I'm so surprised that adding an element in negative index of the array, adds a key-value pair in the array. And also, the length of the array is not incremented?

like image 896
Henok Tesfaye Avatar asked Oct 25 '25 02:10

Henok Tesfaye


2 Answers

Extract the definition from MDN Array.length

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.

It only counts from a 0-based index up to the highest numerical. So anything invalid or negative is ignore. So, the length is not really the number of elements that you visually see.

Relationship with the length property MDN

If the only argument passed to the Array constructor is an integer between 0 and 232-1 (inclusive), this returns a new JavaScript array with its length property set to that number (Note: this implies an array of arrayLength empty slots, not slots with actual undefined values). If the argument is any other number, a RangeError exception is thrown.

Arrays cannot use strings as element indexes (as in an associative array) but must use integers. Setting or accessing via non-integers using bracket notation (or dot notation) will not set or retrieve an element from the array list itself, but will set or access a variable associated with that array's object property collection. The array's object properties and list of array elements are separate, and the array's traversal and mutation operations cannot be applied to these named properties.

Here indicates the differences between setting a value and a property. Anything set beyond the valid range of the array is considered property, just like anything other objects

like image 62
Thanh Trung Avatar answered Oct 27 '25 00:10

Thanh Trung


Arrays are numerically indexed, but the tricky thing is that they also are objects that can have string keys/properties added to them (but which don't count toward the length of the array):

Indexes in an array start from 0 onwards, i.e. they can be positive.

When you try to access a negative index in an array like a[-1] it will act as a key and -1 will get stored as "-1" key in array which is also an object. But it won't count towards the length of the array, only numerical indexes will get counted towards length of the array. You can call array as a subtype of objects in javascript


let a = [];
a[0] = 9;
a[-1] = 10; // will act as key "-1" added to object a with value 10
console.log(a); //[9]
console.log(a["-1"]); //10
console.log("Length of array " + a.length); // 1, key "-1" will not contribute to the lenght of an array

I would recommend you to go through the article

like image 39
Akshay Bande Avatar answered Oct 26 '25 22:10

Akshay Bande