
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
What is the importance of _.initial() function in JavaScript?
_.initial()
_.initial() is a function in underscore.js, which is a library of javascript. This method is used to differentiate the last element of an array from the rest of the elements. This method just ignores the last value of an array.
syntax
_.initial( array, n );
_.Initial() can take 2 parameters.
array - The method takes the array and gives out all the elements except the last element.
n - It is nothing but a number of elements that are to be trimmed from the given array. It is optional.
Example
<html> <body> <script src ="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js" > </script> </head> <body> <script type="text/javascript"> document.write(_.initial([1,2,3,4,5])); </script> </body> </html>
Output
1,2,3,4
If we provide the second parameter i.e a value(number), then the method _.initial() receives that value and trims those number of elements from the array from right to left and displays the remaining elements.
Example
In the following example, 4 is provided as the second parameter so 4 elements will be trimmed and the remaining elements will be displayed. Here since there are 5 elements in the array and the number that is passed as the second parameter is 4, after trimming the elements only 1 element is displayed as shown in the output.
<html> <body> <script src ="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js" > </script> </head> <body> <script type="text/javascript"> document.write(_.initial([1,2,3,4,5],4)); </script> </body> </html>
Output
1
- Related Articles
- What is the importance of Symbol.isConcatSpreadable in JavaScript?
- What is the importance of _isEqual() method in JavaScript?
- What is the importance of _without() method in JavaScript?
- What is the importance of _.union() method in JavaScript?
- What is the importance of Navigator Object in JavaScript?
- What is the importance of str.padStart() method in JavaScript?
- What is the importance of Math.trunc() method in JavaScript?
- What is importance of startsWith() method in JavaScript?
- What is the importance of '/g' flag in JavaScript?
- What is the importance of '/i' flag in JavaScript?
- What is the importance of ES6's template strings in JavaScript?
- What is the importance of '+' operator in type conversion in JavaScript?
- What is the importance of "enumerable" attribute in defining a property in JavaScript object?
- What is the importance of hygiene?
- What is the importance of Computer?
