MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/learnjavascript/comments/qg23w3/negative_array_indexing_using_at_method/hi4jfac/?context=3
r/learnjavascript • u/jd_300 • Oct 26 '21
69 comments sorted by
View all comments
28
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? 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.
6
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? 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.
11
const lastElement = arr[arr.length - 1]
Why wouldn't you just do the above?
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.
0
cause its longer than const lastElement = arr.at(-1) ? also, no need for useless computations in source code
const lastElement = arr.at(-1)
-1 u/HyerOneNA Oct 26 '21 But the at method isn’t widely available on all browsers.
-1
But the at method isn’t widely available on all browsers.
28
u/HyerOneNA Oct 26 '21
Why use the at method when you can just use square bracket notation?