Tutorialspoint.dev

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:

<!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>

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:

<!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>                    

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:

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

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:

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

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.

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



This article is attributed to GeeksforGeeks.org

leave a comment

code

0 Comments

load comments

Subscribe to Our Newsletter