.head and .tail

Gets the first element or all but the first element.

  1. const array = [1, 2, 3]
  2.  
  3. // Underscore: _.first, _.head, _.take
  4. // Lodash: _.first, _.head
  5. _.head(array)
  6. // output: 1
  7.  
  8. // Underscore: _.rest, _.tail, _.drop
  9. // Lodash: _.tail
  10. _.tail(array)
  11. // output: [2, 3]
  12.  
  13.  
  14. // Native
  15. const [ head, ...tail ] = array
  16. console.log(head)
  17. // output: 1
  18. console.log(tail)
  19. // output [2, 3]

Browser Support for Spread in array literals

ChromeEdgeFirefoxIEOperaSafari
46.0 ✔12.0 ✔16.0 ✔37.0 ✔8.0 ✔