34
loading...
This website collects cookies to deliver better user experience
flat
and filter
added. And we're not done yet.const arr = ["a","b","c","d"]
arr[0] // this is "a"
arr[2] // this is "c"
const arr = ["a","b","c","d"]
arr[-1] // This is NOT "d"
-1
is already a valid key. Arrays are really objects with indeces as keys. So arr[-1]
is looking at the arr
object and the value of the "-1"
key, which is undefined
.arr[arr.length - 1] // this is "d"
arr.slice(-1)[0] // this is "d"
at
function is under consideration. Instead of those options, you'd be able to write this instead.arr.at(-1) // this is "d"
at
is that it can replace square brackets all together.arr.at(0) // this is still "a"
arr.at(5) // this is undefined
item
. However, it wasn't web compatible as it clashed with major libraries. So, at
is the current proposal.