Thursday, July 4, 2024
HomeLanguagesJavascriptUnderscore.js _.last() Function

Underscore.js _.last() Function

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


Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

Nicole Veronica Rubhabha
Nicole Veronica Rubhabha
A highly competent and organized individual DotNet developer with a track record of architecting and developing web client-server applications. Recognized as a personable, dedicated performer who demonstrates innovation, communication, and teamwork to ensure quality and timely project completion. Expertise in C#, ASP.Net, MVC, LINQ, EF 6, Web Services, SQL Server, MySql, Web development,
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments