r/learnjavascript • • Oct 26 '21

Negative array indexing using "at" method

Post image
203 Upvotes

69 comments sorted by

View all comments

28

u/HyerOneNA Oct 26 '21

Why use the at method when you can just use square bracket notation?

6

u/jd_300 Oct 26 '21 edited Oct 26 '21

In bracket notation negative indexing is not possible but using at method you can easily navigate to the last index of array. It's just another way to access elements.

11

u/Gibbo3771 Oct 26 '21
const lastElement = arr[arr.length - 1]

Why wouldn't you just do the above?

1

u/HyerOneNA Oct 26 '21

I mean this is what I meant..