In Python, you can do that:
arr = [1,2,3]
arr[-1] // evaluates to 3
But in JS, you can\'t:
let arr = [1,2,3];
arr[-1]; // e
You can use arr[-1]
- it will try to access the -1
property on the arr
object, which is possible when weird code has assigned to the negative index. For example:
const arr = [1,2,3]
arr[-1] = 'foo';
console.log(arr[-1]);
Javascript property access has always worked this way - so, changing things so that [-1]
will refer to the last item in the array would be a breaking change, which the standards strive very hard to avoid. (remember how they backed out of the Array.prototype.flatten
name due to incompatibility with an extremely old and obsolete version of MooTools which still exists on only a few sites - this would be far worse)