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
.slice(-N)[0]
:const array = [1, 2, 3]
console.log(array.slice(-1)[0]) // 3
console.log(array.slice(-2)[0]) // 2
console.log(array.slice(-3)[0]) // 1
String
s you have another option (instead of [0]
).const string = 'ABC'
console.log(string.slice(-1)) // 'C'
console.log(string.slice(-2, -1)) // 'B'
console.log(string.slice(-3, -2)) // 'A'
.substr(-N, 1)
:const string = 'ABC'
console.log(string.substr(-1)) // 'C'
console.log(string.substr(-2, 1)) // 'B'
console.log(string.substr(-3, 1)) // 'A'