Underscore.js | _.last() with Examples

The Underscore.js is a JavaScript library that provides a lot of useful functions like the map, filter, invoke etc even without using any built-in objects.
The _.last() is used to display the last element of the array. It is usually applied to separate the elements of an array by making it into 2 arrays. One which contains only the last element and the other which contains all the elements except the last one.

Syntax:

_.last( array, [n] ) 

Parameters: This function accepts two parameter which are listed below:

  • array: This parameter is used to hold the array elements.
  • n: This parameter is used to hold the last element.

Return value: It returns the last element of the array.

Passing an array of numbers to the _.last() function: The ._last() function takes the elements from the list one by one and ignores them. It only takes the last element from the array and returns it.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
    <head>
        <script src
        </script>
    </head>
    <body>
        <script type="text/javascript">
            console.log(_.last([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]));
        </script>
    </body>
</html>

chevron_right


Output:

Passing an array of alphabets / words to the _.last() function: The ._last() function takes the elements from the list one by one and ignores them. It does not distinguish between the numbers and the words array. It only takes the last element from the array and returns it. Then finally console.log() will display the last element.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
    <head>
        <script src
        </script>
    </head>
    <body>
        <script type="text/javascript">
            console.log(_.last(['html', 'css', 'js',
                        'ajax', 'php', 'node.js']));
        </script>
    </body>
</html>                    

chevron_right


Output:

Passing an array of special characters to the _.last() function: The ._last() function takes the elements from the list one by one and ignores them. It also does not distinguishes between special character array or the numbers array or the array of words. It only takes the last element from the array and returns it.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
    <head>
        <script src
        </script>
    </head>
    <body>
        <script type="text/javascript">
            console.log(_.last(['!', '@', '#', '$', '%', '^']));
        </script>
    </body>
</html>                    

chevron_right


Output:

Passing a heterogeneous array to the _.last() function: Heterogeneous array is an array which contains all kinds of the elements. It will also work the same way. Since the array is having all the elements inside ” so they are considered as character elements. This is the reason why _.initial() does not distinguish between number, character and the special character arrays.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
    <head>
        <script src =
        </script>
    </head>
    <body>
        <script type="text/javascript">
            console.log(_.last(['1', 'javascript', 
                        '#', '2', 'underscore', '^']));
        </script>
    </body>
</html>                    

chevron_right


Output:

Note: These commands will not work in Google console or in Firefox as for these additional files need to be added which they didn’t have added. So, add the given links to your HTML file and then run them.

filter_none

edit
close

play_arrow

link
brightness_4
code

<script type="text/javascript" src
</script

chevron_right




My Personal Notes arrow_drop_up

I like to do coding in C++C and java programming languages HTML and CSS always intersts me Sharing knowleged is the best way according to me to increase ones knwoledge

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.