![]() |
VOOZH | about |
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.
_.last( array, [n] ) It returns the last element of the array.
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.
Output:
👁 ImageThe ._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.
Output:
👁 ImageThe ._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.
Output:
👁 ImageHeterogeneous 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.