Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

11
  • 95
    if you just need last item you can you Array.pop() Commented Jun 7, 2020 at 6:54
  • 146
    @BadriDerakhshan That would inevitably remove the last element from the array before returning it so this is not an option if you merely want to inspect the last element of the array. You would also have to push it back to restore the array to its former state. Commented Nov 1, 2020 at 13:02
  • 33
    The solution from @BadriDerakhshan is useful for cases where the array created is temporary. For eg. instead of x = string.split('/'); return x[x.length-1], you could do return x.split('/').pop() Commented May 27, 2021 at 10:29
  • 5
    Doesn't work on Safari Commented Oct 17, 2021 at 0:55
  • 21
    at() method is not supported by Safari IOS, Chrome IOS, and Samsung Internet caniuse.com/mdn-javascript_builtins_string_at Commented Nov 26, 2021 at 11:04