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*

6
  • 210
    using destructuring is nice too: const [lastItem] = arr.slice(-1) Commented Mar 11, 2019 at 6:30
  • 97
    @Badrush slice() makes new array with a copy of a single element, and pop modifies only that copy. the original array remains unharmed Commented May 7, 2020 at 18:21
  • 8
    @mvmn i did a benchmark a long time ago, throughput was around one or two million calls per second on a single 2.4ghz thread. so unless you have solve problems you shouldn't solve in JS anyways, it won't be noticable (iirc slowdown compared to arr[arr.length-1] was 50-100x) Commented Jun 2, 2020 at 18:54
  • 7
    this method is very slow Commented Jun 21, 2020 at 22:30
  • 16
    @Govan please see my comment. you can call this an exorbitant number of times per second. if you need more, or have the array length readily available, use normal array access for maximum performance arr[arr.length-1] Commented Jun 22, 2020 at 20:14