We now have the at()
method in JavaScript, which allows us to access individual array elements:
const fib = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55];
fib.at(0); // 0
fib.at(-1); // 55
fib.at(-2); // 34
Previously, to access the last element in the array we would have had to do this:
const fib = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55];
fib[fib.length - 1]; // 55
The new at()
method is much more readable.