_.first

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

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

Browser Support for Array.prototype.slice()

ChromeEdgeFirefoxIEOperaSafari
1.0 ✔1.0 ✔