Open In App

Underscore.js _.last() Function

Improve
Improve
Like Article
Like
Save
Share
Report

Underscore.js _.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:

  • 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: The below code expalains the use of the _.last() method with an array of numbers.

html




<!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: The below code example implements the_.last() function with the string or alphabets.

html




<!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: The below example explains the behaviour of the _.last() function with the special characters.

html




<!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: The below code uses the _.last() functio with the heterogeneous array.

html




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


Output:



Last Updated : 17 Jan, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads