Another small function I just learned about. myArray.at(x)
works almost like myArray[x]
. But the new .at() method has one awesome advantage. It supports negative indices which lets you access an array from the end. Negative indices with the conventional method would result in undefined
const myArray = [
'Banana',
'Apple',
'Mango',
'Pineapple'
]
console.log(myArray.at(-1)) // will result "Pineapple"
Read more about it here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/at
Unfortunately Safari doesn't support the .at() method yet. But what did I expect? 😫