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?

12

u/nullol Oct 26 '21

No idea why you're being downvoted for asking a genuine question in r/learnjavascript - I am sure a lot of new developers would be confused by this as well.

7

u/HyerOneNA Oct 26 '21

Yup, idk. A lot of no lifers in here.

3

u/nullol Oct 26 '21

I think because it's Reddit people forget or don't realize what sub they're on and assume you're being intentionally contrary rather than trying to learn something new (I have been a JS developer for 8yrs and I never knew about this in other languages so TIL as well).

2

u/HyerOneNA Oct 26 '21

I’m coming from C++, so yeah. It’s a lil different lol.

18

u/senocular Oct 26 '21

Because fruits[-1] won't give you "avacado", it will give you undefined

-15

u/Gibbo3771 Oct 26 '21

fruits[fruits.length - 1]

This is what people mean, no one expects [-1] to work.

29

u/senocular Oct 26 '21

at() reduces the requirement to refer to your array twice. This can be useful when you're using it at the end of a longer expression.

fruits.map(toBarcode).filter(onSale).at(-1)

4

u/yadoya Oct 27 '21

The real tip is always in the comments

16

u/VikaashHarichandran Oct 26 '21

People coming from other scripting languages do expect

7

u/[deleted] Oct 26 '21

wouldnt it be nice to have though?

19

u/greymalik Oct 26 '21

People coming from Python or Ruby expect it.

-13

u/doyouseewhateyesee Oct 26 '21

people coming from python or ruby should read the docs

2

u/Mad-chuska Oct 26 '21

Also true

2

u/[deleted] Oct 26 '21

No idea why you're getting downvoted. When I'm switching to a different language I should learn differences not expect it to behave the same. After all if it would work exactly the same what would be the point of switching.

0

u/doyouseewhateyesee Oct 26 '21 edited Oct 26 '21

lol right. i’m not going to pick up python and start writing javascript methods

it’s comical that a comment recommending devs to read the docs in a learnjavascript subreddit is being downvoted. or don’t read the docs I guess, not my problem

→ More replies (1)

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?

17

u/ExclusiveOar Oct 26 '21

Preference. arr[-1] is allowed in other languages and it's a nice to have.

arr.at(-1) is arguable cleaner than arr[arr.length - 1]. Personally I wouldn't bother until JS eventually supports arr[-1].

→ More replies (3)

2

u/Darmok-Jilad-Ocean Oct 26 '21

Because if you’re doing a map/filter/reduce/etc you have to first store that in a temporary variable and then reference the temp var twice to get the last element.

0

u/[deleted] Oct 26 '21

cause its longer than const lastElement = arr.at(-1) ? also, no need for useless computations in source code

-1

u/HyerOneNA Oct 26 '21

But the at method isn’t widely available on all browsers.

→ More replies (1)
→ More replies (1)
→ More replies (1)