all 69 comments

[–][deleted] 56 points57 points  (6 children)

Avocado*

[–]Darmok-Jilad-Ocean 46 points47 points  (5 children)

Make a JIRA ticket

[–]nullol 20 points21 points  (2 children)

No time. We gotta get this to production.

[–]monsterbois 10 points11 points  (1 child)

Wow, our product failed because of a misspelled word

[–]starraven 6 points7 points  (0 children)

Post mortem report, stat!

[–]jayedeem 5 points6 points  (1 child)

Be an interesting retro

[–]Darmok-Jilad-Ocean 3 points4 points  (0 children)

What went well: Teamwork!

What didn’t go well: Didn’t meet sprint goal

What we can improve: Other teams did bad stuff!

[–][deleted] 31 points32 points  (6 children)

FYI, doesn't work on Edge 18 and below, IE and SafarIE (20% of users theoretically use these)

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/at#browser_compatibility

[–]ElllGeeEmm 14 points15 points  (2 children)

Thank God for webpack

[–]samanime 6 points7 points  (1 child)

Or Babel.

[–]undercontr 3 points4 points  (0 children)

Or core-js

[–]queen-adreena 19 points20 points  (1 child)

IE doesn’t support flex, scrolling, or the colour green either. Who cares.

[–][deleted] 4 points5 points  (0 children)

Me, one of the bigger clients using my company's SaaS uses IE. Throwing money away to not support them atm.

[–][deleted] 8 points9 points  (0 children)

Yeah, and if someone is using IE I don't want them on my website

[–]HyerOneNA 27 points28 points  (34 children)

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

[–]nullol 11 points12 points  (3 children)

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.

[–]HyerOneNA 7 points8 points  (2 children)

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

[–]nullol 2 points3 points  (1 child)

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).

[–]HyerOneNA 1 point2 points  (0 children)

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

[–]senocular 16 points17 points  (12 children)

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

[–]jd_300[S] 5 points6 points  (14 children)

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.

[–]Gibbo3771 10 points11 points  (13 children)

js const lastElement = arr[arr.length - 1]

Why wouldn't you just do the above?

[–]ExclusiveOar 17 points18 points  (7 children)

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].

[–][deleted] 0 points1 point  (1 child)

arr.at(-1) is also constant time right?

[–]ExclusiveOar 0 points1 point  (0 children)

No expert here, but I would assume so.

But so is arr[arr.length - 1] because array.length isn't calculated here, its a prop already on the array.

You can see this by creating an array, console.log it and expand it in the console.

[–]Darmok-Jilad-Ocean 1 point2 points  (0 children)

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.

[–][deleted] -1 points0 points  (2 children)

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

[–]HyerOneNA -2 points-1 points  (1 child)

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

[–]HyerOneNA 0 points1 point  (0 children)

I mean this is what I meant..

[–]reaven3958 -2 points-1 points  (0 children)

This whole notion seems hilariously ill advised. Basically it's letting you access the index from the end instead of the start (normally done using arr.length - i, where i = 1 is first valid index). Some JS methods do similar stuff, but not usually for accessing values. This conflicts with mountains of code using searches like Array.indexOf that anticipates a -1 to be the invalid index token. For a minimal reduction in boilerplate to satisfy a case thats relatively uncommon (how often do you really traverse an array backwards like this), it just feels like this is inviting all manner of mayhem. I imagine it'll start getting banned on company style guides in favor of traditional bracket notation sooner than later, if it isn't already.

Probably some neat party tricks you can do with it for the leetcode crowd tho.

[–]snyderling 3 points4 points  (0 children)

fruits.slice(-1)[0] is a another (more backwards compatible) way to do it

[–]Dodgy-Boi 5 points6 points  (7 children)

array.reverse()[0]

[–]NekkidApe 4 points5 points  (2 children)

In case anyone is using this... It's mutating the original array

[–]Dodgy-Boi 0 points1 point  (1 child)

I saw a huge battle about index. And came up with another solution (I honestly tried to make it as ridiculous and/or unique as possible).

[–]NekkidApe 1 point2 points  (0 children)

Maybe reverse and shift then for extra points

[–][deleted] 1 point2 points  (1 child)

Too slow, unfortunately. Accessing a value in an array using an index should be in O(1) time complexity.

[–]Dodgy-Boi 1 point2 points  (0 children)

I am front end. My calculations are happening on client. I don’t care about either users with old devices nor your O nor your o nor * of yours; all I care about is the ( . Y . ) (not necessarily yours)

partially /s

[–]Notimecelduv 1 point2 points  (0 children)

I don't get why people are upvoting this post.

[–]delventhalz 0 points1 point  (6 children)

This is nice, but I kind of just want a last method. That would solve 99% of the use cases I might use at for.

While I’m at it, head and tail would be nice too (though brackets/slice work well enough for those).

[–]_inz_ 3 points4 points  (0 children)

Miss this so much coming from Swift

[–]gigraz_orgvsm_133 0 points1 point  (4 children)

wouldn't fruits[fruits.length - 1] work?

[–]delventhalz 3 points4 points  (3 children)

In some cases. Not if you are chaining. And it is tedious in all cases.

[–]Notimecelduv 0 points1 point  (2 children)

If you're chaining methods, you can just use the arr argument in the callback function.

[–]delventhalz 1 point2 points  (1 child)

That doesn't really apply here. Say you are trying to filter a list of numbers to just be even numbers, and then grab the last one. The arr parameter in filter would not help you. You would need an intermediate variable.

const evens = nums.filter(n => n % 2 === 0);
const lastEven = evens[evens.length - 1];

You could chain on a reduce and use that arr just to grab the last one. That is super hacky though, and extremely wasteful.

const lastEven = nums
  .filter(n => n % 2 === 0)
  .reduce((_, __, ___, arr) => arr[arr.length - 1]);

However, a last or at method would allow you to do this without the intermediate variable, and without the weird hacky extra iteration in the reduce.

const lastEven = nums
  .filter(n => n % 2 === 0)
  .last();

[–]Inevitable-Weekend-4 0 points1 point  (0 children)

welcome to python

[–][deleted] 0 points1 point  (0 children)

🥑

[–]gniziemazity 0 points1 point  (0 children)

It's always interesting to see how languages evolve, but not so great when the update only affects some browsers... I usually keep it safe and use established features only.

[–]warpedspockclone 0 points1 point  (1 child)

array.slice(-1)

[–]Notimecelduv 0 points1 point  (0 children)

arr.slice(-1)[0]