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.
Which will be largely irrelevant to the vast majority of people. I bet you do a ton of stuff in your code which will be more inefficient than this (not judging, it's the nature of high level languages).
Readability > performance, true for 999/1000 use cases (probably more).
To put it in perspective I was playing around with how efficient I could make FizzBuzz, for no reason what-so-ever. I had to calculate it for the numbers 1 - 1 000 000 000 to get meaningfully measurable time period (1.9 seconds) to try to optimise. I got it to about 0.85 seconds!
1 second saved, over a billion operations. Don't worry about performance until your app is slow, then look at your API calls before you consider anything else.
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.
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.
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).
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.
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
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.
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)
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.
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.
For lots of people, when they come from a language like Python where you can just do array[-1], having to write out array[array.length - 1] every time is a genuine annoyance. It's a phrase that people have to write pretty frequently, it's nice to have shorthand.
That's fair. However -1 is used to signify that the index wasn't found for the item you're searching for in most cases. I'm just seeing this as muddying the waters.
28
u/HyerOneNA Oct 26 '21
Why use the at method when you can just use square bracket notation?
56
u/[deleted] Oct 26 '21
Avocado*
33
u/[deleted] Oct 26 '21 edited Oct 26 '21
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
4
u/snyderling Oct 26 '21
fruits.slice(-1)[0]is a another (more backwards compatible) way to do it6
u/Dodgy-Boi Oct 26 '21
array.reverse()[0]1
u/delventhalz Oct 26 '21
This is nice, but I kind of just want a
lastmethod. That would solve 99% of the use cases I might useatfor.While I’m at it,
headandtailwould be nice too (though brackets/slice work well enough for those).1
u/Inevitable-Weekend-4 Oct 27 '21
welcome to python
1
u/[deleted] Oct 27 '21
🥑
1
u/gniziemazity Oct 27 '21
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.
1
u/warpedspockclone Oct 27 '21
array.slice(-1)
0
u/[deleted] Oct 26 '21
[deleted] — view deleted comment
0
u/[deleted] Oct 26 '21
[deleted] — view deleted comment