Getting last array element with arr[arr.length – 1] is verbose. Use .at() instead.
Old Way:
const arr = [10, 20, 30, 40, 50]; const last = arr[arr.length - 1]; // 50 const secondLast = arr[arr.length - 2]; // 40
New Way:
const last = arr.at(-1); // 50 const secondLast = arr.at(-2); // 40 const first = arr.at(0); // 10
Negative indices count from end. Clean and readable!
Browser Support: All modern browsers (2022+)
