_.last

Returns the last element of an array. Passing n will return the last n elements of the array.

  1. // Underscore/Lodash
  2. const numbers = [1, 2, 3, 4, 5];
  3. _.last(numbers);
  4. // => 5
  5.  
  6. _.last(numbers, 2);
  7. // => [4, 5]
  8.  
  9. // Native
  10. const numbers = [1, 2, 3, 4, 5];
  11. numbers[numbers.length - 1];
  12. // => 5
  13. //or
  14. numbers.slice(-1)[0];
  15. // => 5
  16. //or
  17. [].concat(numbers).pop()
  18. // => 5
  19.  
  20. // Native (works even with potentially undefined/null)
  21. [].concat(undefined).pop()
  22. // => undefined
  23.  
  24. numbers.slice(-2);
  25. // => [4, 5]

Browser Support for Array.prototype.concat()

ChromeEdgeFirefoxIEOperaSafari
1.0 ✔1.0 ✔5.5 ✔