Lodash _.first() and _.last() Method Examples

The .first()/.head() functions return the first array element; the _.last() function returns the last array element.

Learn Lodash JS at Lodash JS Tutorial with Examples.

Lodash _.first() and _.last() Method Examples

<!DOCTYPE html>
    <title>Lodash Tutorial</title>
    <script src="https://cdn.jsdelivr.net/npm/lodash@4.17.11/lodash.min.js"></script>
    <script type="text/javascript">
  // Lodash first and last array elements
        words = ['sky', 'wood', 'forest', 'falcon',
            'pear', 'ocean', 'universe'

        let fel = _.first(words);
        let lel = _.last(words);

        console.log(`First element: ${fel}`);
        console.log(`Last element: ${lel}`);
The above HTML prints below output on the console:
First element: sky
Last element: universe