Checkout my CSS Grid By Example and Getting Started With React video courses on Skillshare. Sign up now and get one free month of Skillshare.

Using the new method

There is a new array method on the block. It's and the main difference vs the normal square brackets access syntax is the support for the negative indexes.

Let's consider the following array:

const legumes = [

At a first glace [] and at() will do the same thing:

legumes[0] // "beans" // "beans"

But while legumes[-1] will end up in returning undefined the will return "lentils".

legumes[-1] // undefined // "lentils"

With the classic square brackets syntax, we will need to use a trick like the following to access the last element in the array:

legumes[legumes.length - 1] // lentils

The negative indexes in the at() method will give us a more natural way to access elements at the end of the array: // "clover" // "peanuts"

Keep in mind that for the moment Safari does not yet support negative indexes.

Home Screencasts Best of Newsletter Search X