Why can't I do array[-1] in JavaScript?

前端 未结 4 1522
春和景丽
春和景丽 2021-01-15 00:36

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         


        
4条回答
  •  Happy的楠姐
    2021-01-15 01:05

    You miss the point, that arrays are objects (exotic object) and -1 is a valid key.

    var array = [1, 2, 3];
    
    array[-1] = 42;
    
    console.log(array);
    console.log(array[-1]);

提交回复
热议问题