In javascript I define an array like this
var arr = [1,2,3];
also I can do
arr[-1] = 4;
Now if I do
arr = undefined;
I also lose the reference to the value at arr[-1].
SO for me logically it seems like arr[-1] is also a part of arr.
But when I do following (without setting arr to undefined)
arr.length;
It returns 3 not 4;
So my point is if the arrays can be used with negative indexes, these negative indexes should also be a part of their length**. I don't know may be I am wrong or I may be missing some concept about arrays.
JavaScript arrays are collections of items, where each item is accessible through an index. These indexes are non-negative integers, and accessing a negative index will just return undefined .
No it cannot. MDN says:. The value of the length property is an integer with a positive sign and a value less than 2 to the 32nd power (232).
As others said, In Javascript array[-1] is just a reference to a property of array (like length ) that is usually undefined (because it's not evaluated to any value).
Python programming language supports negative indexing of arrays, something which is not available in arrays in most other programming languages. This means that the index value of -1 gives the last element, and -2 gives the second last element of an array. The negative indexing starts from where the array ends.
Negative indexes in JavaScript arrays using Proxies. JavaScript arrays are collections of items, where each item is accessible through an index. These indexes are non-negative integers, and accessing a negative index will just return undefined.
Negative indexes aren't allowed in arrays, so our -1 isn't actually an index. Instead, it's being converted into the string '-1' and stored as a normal object property.
I'm storing objects for a grid in a two dimensional array. Since the grid is hexagonal, it is much easier to use a coordinate system that is centered at (0, 0) and ranges from -r to r. According to this thread, apparently negative indices in an array aren't real indices, but actually properties of the array.
If we loop over the array with forEach, we won't see the "negative indexes" because they're not actually array indexes. This "negative index" behavior may seem unimportant. But it can cause some very subtle bugs! Here's an example. The function below returns the first array element greater than 2.
SO for me logically it seems like arr[-1] is also a part of arr.
Yes it is, but not in the way you think it is.
You can assign arbitrary properties to an array (just like any other Object in JavaScript), which is what you're doing when you "index" the array at -1
and assign a value. Since this is not a member of the array and just an arbitrary property, you should not expect length
to consider that property.
In other words, the following code does the same thing:
var arr = [1, 2, 3];
arr.cookies = 4;
alert(arr.length) // 3;
The length
property will return a number one higher than the highest assigned "index", where Array "indexes" are integers greater than or equal to zero. Note that JS allows "sparse" arrays:
var someArray = [];
someArray[10] = "whatever";
console.log(someArray.length); // "11"
Of course if there are no elements then length
is 0
. Note also that the length
doesn't get updated if you use delete
to remove the highest element.
But arrays are objects, so you can assign properties with other arbitrary property names including negative numbers or fractions:
someArray[-1] = "A property";
someArray[3.1415] = "Vaguely Pi";
someArray["test"] = "Whatever";
Note that behind the scenes JS converts the property names to strings even when you supply a number like -1
. (The positive integer indexes also become strings, for that matter.)
Array methods, like .pop()
, .slice()
, etc., only work on the zero-or-higher integer "indexes", not on other properties, so length
is consistent on that point.
Note that when you use a position (or 0) index, values are placed within the array:
var array = [];
array[0] = "Foo";
array[1] = "Bar";
// Result: ["Foo", "Bar"]
// Length: 2
This is not the case when you add non-index values (not 0-9+):
var array = [];
array[0] = "Foo";
array[1] = "Bar";
array[-1] = "Fizzbuzz"; // Not a proper array index - kill it
// Result: ["Foo", "Bar"]
// Length: 2
Values are only placed in the array when you play by the rules. When you don't, they aren't accepted. They are however accepted on the Array object itself, which is the case with just about anything in JavaScript. Even though ["Foo", "Bar"]
are the only values in our array, we can still access "Fizzbuzz"
:
array[-1]; // "Fizzbuzz"
But note again that this isn't part of the array values, since its "index" isn't valid. It was instead added onto the array as just another member. We could access other array members in the same fashion:
array["pop"]; // function pop() { [native code] }
Note here that we're accessing the pop
method on the array, which informs us that this contains native code. We're not accessing any of the array values with a key of "pop", but rather a member on the array object itself. We can further confirm this by cycling over the public members of the object:
for (var prop in array)
console.log(prop, array[prop]);
Which spits out the following:
0 Foo
1 Bar
-1 Fizzbuzz
So again, it's on the object, but it's not in the array.
Awesome question! Caused me to do a double-take for sure.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With