Underscore.js | _.initial() 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 _.initial() function is used to exclude the last element from he array. This function is used to perform some action on all the elements except last element. This is a basic function which helps to differentiate between the last element of the array and all the rest elements.

Syntax:

_.initial( 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 n.

Return value: It returns an array except the last element.

Passing a list of numbers to _.initial() function: The ._initial() function takes the element from the list one by one and then add this element to the resultant array. It is used to print all the elements except 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(_.initial([1, 2, 3, 4, 5, 
                            6, 7, 8, 9, 10]));
        </script>
    </body>
</html>                    

chevron_right


Output:

Passing words to the _.initial() function: The ._initial() function takes the element which is here alphabets from the list one by one and then add this element to the resultant array. The resultant array also gives the length of the resultant array. Then console.log() the resultant array.

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(_.initial(['html', 'css', 'js', 
                            'ajax', 'php', 'node.js']));
        </script>
    </body>
</html>                    

chevron_right


Output:

Using the second parameter of the _.initial() function: The second parameter is used to select the elements from the array except the number of elements which is given in second parameter and display them. The second parameter is very crucial to display all the elements except the number given in the second parameter from the right side in the array passed.

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(_.initial(['!', '@', '#', 
                            '$', '%', '^']), 4);
        </script>
    </body>
</html>                    

chevron_right


Output:

Using both the words and the numbers in the _.initial() function: It takes the elements from the zeroth index of the given array and displays all the element. The fact that the array elements are words, alphabets or the numbers is not important. The last element is not included in the list.

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(_.initial(['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.