at() method in JavaScript

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.

Latest Today I Learned Posts
HTML image decoding attribute Human readable relative date strings in standard JavaScript at() method in JavaScript Barcode Detection API