Underscore.js Introduction Last Updated : 29 Nov, 2021 Comments Improve Suggest changes Like Article Like Report Underscore.js is a lightweight JavaScript library and not a complete framework that was written by Jeremy Ashkenas that provides utility functions for a variety of use cases in our day to day common programming tasks. With just under six kilobytes in size, this library basically provides us with a whole bunch of useful JavaScript functions for making our life easier, there are literally hundreds of different functions available that support both our workaday functional helpers such as the map and filter functions, as well as more specialized ones such as JavaScript templating, function binding, deep equality testing, creating quick indexes, and many more. Underscore functions basically falls under four major categories which are functions that can be used for manipulating arrays, functions that can be used for manipulating objects, functions that can be used for manipulating both arrays as well as objects and functions that can be used for manipulating other functions itself. Features of Underscore: Perform common operations of data like arrays, objects, JSON files etc.Compatible with other JS libraries like jQuery to perform DOM operation.Contains function for data manipulation. Kinds of Data Operation: Fetch Data from Collection.Transform Data in various formats.Sort, Filter and Group Data.Convert Data into Required result by performing specific operation on them.Templates can be used to perform data operations. Installation: Node.js: npm install underscoreMeteor.js: meteor add underscoreRequire.js: require(["underscore"], ...Bower: bower install underscoreExtendScript: #include "underscore.js" Comment More infoAdvertise with us Next Article Underscore.js _.each() Function cybercreed010 Follow Improve Article Tags : JavaScript Web Technologies Write From Home JavaScript - Underscore.js Similar Reads Underscore.js What is Underscore.js? Underscore.js is a lightweight JavaScript library and not a complete framework that was written by Jeremy Ashkenas. It provides utility functions for a variety of use cases in our day-to-day common programming tasks. Underscore.js provides a lot of features that make our task 4 min read Underscore.js Introduction Underscore.js is a lightweight JavaScript library and not a complete framework that was written by Jeremy Ashkenas that provides utility functions for a variety of use cases in our day to day common programming tasks. With just under six kilobytes in size, this library basically provides us with a w 2 min read Underscore.js Collections FunctionsUnderscore.js _.each() FunctionUnderscore.js is a JavaScript library that provides a lot of useful functions that helps in the programming in a big way like the map, filter, invoke etc even without using any built-in objects. The _.each() function is an inbuilt function in Underscore.js library of JavaScript which is used to retu 3 min read Underscore.js _.map() FunctionThe Underscore.js is a JavaScript library that provides a lot of useful functions that helps in the programming in a big way like the map, filter, invoke etc even without using any built-in objects. The _.map() function is an inbuilt function in Underscore.js library of the JavaScript which is used 4 min read Underscore.js _.reduce() FunctionUnderscore.js _.reduce() function is an inbuilt function in Underscore.js that is used to transform an array's/object's properties into one single value or is used to create a single result from a given list of values. When all the elements of the list are passed to the function/iterate and no more 3 min read Underscore.js _.reduceRight() FunctionThe _.reduceRight() function is an inbuilt method in Underscore.js which is used to perform operations on each element of the list from the right. When all the elements of the list are passed from right to left to the function/iterate and no more elements remain then the _.reduceRight loop ends. It 3 min read Underscore.js _.find() FunctionUnderscore.js _.find() function looks at each element of the list and returns the first occurrence of the element that satisfies the condition. If any element of the list does not satisfy the condition then it returns the undefined value. Syntax:_.find(list, predicate, [context])Parameters:list: Thi 1 min read Underscore.js _.filter FunctionUnderscore.js _.filter() is used to check which elements in the passed array satisfy the condition. It will form a new array of all those elements that satisfy the condition passed from the array. It is mostly used when needed to find certain elements from a large array. Syntax:_.filter( list, predi 4 min read Underscore.js _.where() FunctionUnderscore.js _.where() function is used to find all the elements that match the searching condition. Suppose to find all the student details of a class then apply the _.where() function to the list of all the sections and pass the condition as the section name. So, the names of all the students in 4 min read Underscore.js _.whereWhere FunctionUnderscore.js _.findWhere() function is used to have a list of all the elements that match the given property. The _.findWhere() function is used to search a content in the whole list of sections. The section which contains the content will be displayed. Syntax:  _.findWhere(list, properties);Param 3 min read Underscore.js _.reject FunctionThe Underscore.js is a JavaScript library that provides a lot of useful functions that helps in the programming in a big way like the map, filter, invoke etc even without using any built-in objects. The _.reject() function is used to give the answer which does not match with the given condition. It 3 min read Underscore.js _.every FunctionThe Underscore.js is a JavaScript library that provides a lot of useful functions that helps in the programming in a big way like the map, filter, invoke etc even without using any built-in objects. The _.every() function is used to test all the elements of the list can pass the given test. It stops 4 min read Underscore.js _.some FunctionUnderscore.js _.some() function is used to find whether any value in the given list matches the given condition or not. If at least one value satisfies this condition then the output will be true. When none of the values matches then the output will be false. Syntax: _.some(list, [predicate], [cont 4 min read Underscore.js _.contains FunctionUnderscore.js _.contains() function is used to check whether a particular item is given in the list or not. This function needs to pass the list to this function. If the list contains a large of items then simply define the list earlier and pass the name of the list as an argument to the _.contains( 3 min read Underscore.js _.invokes FunctionThe Underscore.js is a JavaScript library that provides a lot of useful functions that helps in the programming in a big way like the map, filter, invoke etc even without using any built-in objects. The _.invoke() function is used to perform certain actions like sorting, joining, joining with some s 3 min read Underscore.js _.pluck FunctionThe Underscore.js is a JavaScript library that provides a lot of useful functions that helps in the programming in a big way like the map, filter, invoke, etc even without using any built-in objects. The _.pluck() function is used when we need to extract a list of a given property. Like we have to f 4 min read Underscore.js Collections Complete ReferenceUnderscore.js is a lightweight JavaScript library and not a complete framework that was written by Jeremy Ashkenas that provides utility functions for a variety of use cases in our day-to-day common programming tasks. Underscore.js Collection functions are used on arrays, objects, and array-like obj 2 min read Underscore.js Arrays FunctionsUnderscore.js _.first() FunctionUnderscore.js  _.first() function is used to return the first element of the array, i.e. the number at the zeroth index. It returns the first n elements in the array of m size (n < m) by just passing the variable n in the array. It is a very easy-to-use function of the underscore.js library and i 3 min read Underscore.js _.initial() FunctionThe 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 the array. This function is used to perform some action on all the elements ex 3 min read Underscore.js _.last() FunctionUnderscore.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] ) Para 3 min read Underscore.js _.rest() FunctionThe 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 _.rest() is used to return the rest of the elements except the zeroth indexed element. Second parameter is used to start the search from the 3 min read Underscore.js _.compact() FunctionUnderscore.js _.compact() function is used to return an array after removing all the false values. The false values in JavaScript are NaN, undefined, false, 0, null, or an empty string. Its output is an array containing all the even values like the elements of the array, numbers, alphabets, characte 3 min read Underscore.js _.flatten() FunctionUnderscore.js _.flatten() function is an inbuilt function in the Underscore.js library of JavaScript that is used to flatten an array that is nested to some level. The resultant array will have no depth. It will be completely flattened. If pass the shallow parameter then the flattening will be done 4 min read Underscore.js _.without() FunctionUnderscore.js _.without() function is used to return a copy of the array which contains all the array except values. Syntax:_.without( array, *values );Parameters:array: This parameter is used to hold the list of array elements.values: This parameter is used to hold the value that needs to be remove 3 min read Underscore.js _.union() FunctionUnderscore.js _.union() function is used to take n number of arrays and return a new array with the unique terms in all those arrays (union of all array). In the new array, the order of the elements is the same as it is mentioned in all the passed arrays. The first occurrence of each array is only i 3 min read Underscore.js _.intersection() FunctionUnderscore.js _.intersection() function is used to find the intersection of passed arrays, i.e. the elements that are common in all the n passed arrays in the _.intersection() function. This function finds the elements that are present in all the arrays and then apply some operation on those element 3 min read Underscore.js _.difference() FunctionUnderscore.js _.difference() function returns the values of the array that are not present in the other arrays. If the element is present in the array then this element will not be included in the resultant array. This is because it takes a difference between the second array and the first array. Sy 3 min read Underscore.js _.uniq() FunctionUnderscore.js _.uniq() function returns the array which does not contain duplicate elements. The first occurrence of the element is included in the resultant array. The operation of checking whether the array is duplicate or not. It is done by the '===' operation. Syntax:_.uniq( array, [isSorted], 3 min read Underscore.js _.zip() FunctionThe 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 _.zip() function matches each passed array of elements to the next passed array element. It is used when more than one homogeneous arrays (o 3 min read Underscore.js _.unzip() FunctionThe Underscore.js is a JavaScript library that provides a lot of useful functions like the map, filter, invokes, etc even without using any built-in objects. The _.unzip() function is used to combine the same type of elements of different arrays into a single array. It matches one by one each passed 4 min read Underscore.js _.object() Function_.object() function: It converts the array elements into objects. We can either pass all keys and then all the values or we can pass the key along with their value in pairs. It is used if we have more than one array but we want to make/ form a relation between these arrays. Also, size of the arrays 3 min read Underscore.js Array Complete ReferenceUnderscore.js is a lightweight JavaScript library and not a complete framework that was written by Jeremy Ashkenas that provides utility functions for a variety of use cases in our day-to-day common programming tasks. Array is a single variable that is used to store different elements. It is often u 2 min read Underscore.js FunctionsUnderscore.js _.bind() FunctionUnderscore.js _.bind() function is used to bind a function to an object. When the function is called, the value of this will be the object. Syntax:_.bind(function, object, *arguments);Parameters:function: This parameter holds the function that needs to be executed. object: This parameter holds the o 2 min read Underscore.js _.bindAll() FunctionBindAll() function in underscore.js is used to bind the number of methods on the object. Each method is given a method name. It is handy to work with the event handlers. Syntax: _.bindAll(object, *methodNames) Parameters: Object: It is the object that contains different methods and functions to be b 2 min read Underscore.js _.partial() FunctionThe _.partial() function is used to apply partially a function by filling in any number of its arguments, without changing its dynamic value. Syntax: _.partial(function, *arguments) Parameters: This function accept two parameters as mentioned above and described below: function: The function that ne 1 min read Underscore.js _.memoize() FunctionThe _.memoize() function is used to memorize a given function by caching the result computed by the function. It is used to speed up for the slow running process. Syntax: _.memoize(function, [hashFunction]) Parameters: This function accepts two parameters as mentioned above and described below: func 1 min read Underscore.js _.delay() FunctionUnderscore.js _.delay() function executes the mentioned function in its argument after waiting for the specified milliseconds. It is mostly used when we want to perform some task but after a certain amount of time. In this case, we can define this function, and then it will be executed after the wai 4 min read Underscore.js _.defer() FunctionUnderscore.js _.defer() function is used to invoke/call a function until the current call stack is cleared. Its major advantage is that it performs expensive computations, calculations, or HTML in chunks without blocking the UI threads from updating. It has a similar functioning to the setTimeOut() 3 min read Underscore.js _.once() FunctionUnderscore.js _.once function is used in conditions where we want a particular function to be executed only a single time. Even though we execute or call this function multiple times then also it will have no effect. The original function's values will only be returned each time it is called. It is 5 min read Underscore.js _.after() FunctionUnderscore.js is a JavaScript library that provides a lot of useful functions that help in the programming in a big way like the map, filter, invoke, etc even without using any built-in objects. The _.after() function is an inbuilt function in Underscore.js library of JavaScript which is used to cre 2 min read Underscore.js _.before() Function_.before() function in underscore.js is used to call a particular function a particular number of times. It uses a count variable that keeps the track of number of times the function is called. Syntax: _.before(count, function) Parameters: Count: It is the number of times a function is to be called. 2 min read Underscore.js _.wrap() FunctionUnderscore.js _.wrap() is used to wrap a function inside another function. It means that the first calling function will be invoked first and then the called function will be executed. If the calling function does not call the called function then the second function will not be executed. Syntax:_.w 3 min read Underscore.js _.negate() FunctionUnderscore.js is a JavaScript library that provides a lot of useful functions that help in the programming in a big way like the map, filter, invoke, etc even without using any built-in objects. The _.negate() function is an inbuilt function in Underscore.js library of JavaScript which is used to fi 1 min read Underscore.js _.compose() FunctionThe _.compose() function is used to return the composition of the list of functions. The list of the function returns the value of the function that follows. Syntax: _.compose(*functions) Parameters: This function accept a single parameter as mentioned above and described below: functions: It contai 1 min read Underscore.js _.restArguments() FunctionUnderscore.js is a JavaScript library that provides a lot of useful functions that help in the programming in a big way like the map, filter, invoke, etc. even without using any built-in objects. The _.restArguments() function is an inbuilt function in Underscore.js library of JavaScript which is us 2 min read Underscore.js Functions Complete ReferenceUnderscore.js is a lightweight JavaScript library and not a complete framework that was written by Jeremy Ashkenas that provides utility functions for a variety of use cases in our day-to-day common programming tasks. Underscore provides a number of functions that can be applied to the elements. The 2 min read Underscore.js Object FunctionsUnderscore.js _.keys() FunctionUnderscore.js_.keys() function is used to return the list of all keys of the given object. Syntax:_.keys( object );Parameters:object: It contains the object elements.Return Value: This function returns the list of all keys of the given object. Example 1: This example shows the use of the _.keys() fu 1 min read Underscore.js _.allKeys() FunctionThe _.allKeys() function is used to return all keys of the object and inherited properties. Syntax: _.allKeys( object ) Parameters: This function accept oa single parameter as mentioned above and described below: object: It contains the object element that holds the elements of key and value pair. R 1 min read Underscore.js _.values() FunctionThe _.values() function is used to return the list of all values of the object element. Syntax: _.values( object ) Parameters: This function accepts one parameter as mentioned above and described below: object: It contains the object element that holds the elements of key and value pair. Return Valu 1 min read Underscore.js _.mapObject() FunctionThe _.mapObject() function is similar to the map but used for the object. This function transforms each value of the object based on the given function/operation. Syntax: _.mapObject(object, iteratee, [context]) Parameters: This function accepts three parameters as mentioned above and described belo 1 min read Underscore.js _.pairs() FunctionUnderscore.js _.pairs() function is used to convert an object into an array of arrays that contain the [key, value] pairs of the object as elements. Syntax:_.pairs( object );Parameters:object: It contains the object element that holds the elements of key and value pair.Return Value:It returns the ar 1 min read Underscore.js _.invert() FunctionUnderscore.js _.invert() function is used to return the copy of an object where the object key is converted into value and object value is converted into the key. It means the [key, value] of the object is reversed. Syntax:_.invert( object );Parameters:object: It contains the object element that hol 1 min read Underscore.js _.create() FunctionUnderscore.js is a JavaScript library that provides a lot of useful functions that helps in the programming in a big way like the map, filter, invoke etc even without using any built-in objects. The _.create() function is an inbuilt function in Underscore.js library of JavaScript which is used to cr 2 min read Underscore.js _.functions() FunctionUnderscore.js _.functions() function is used to return the sorted list of all methods that are present in an object. Syntax:_.functions(object);Parameters: object: It contains the object element that holds the elements [key, value] pair.Return Value: It returns the sorted list of all methods that ar 1 min read Underscore.js _.findKey() FunctionThe _.findKey() function is used to return the key value where the predicate logic or condition returns the truth value or undefined. The predicate function/condition is transformed through iteratee to facilitate shorthand syntaxes. Syntax: _.findKey(object, predicate, [context]) Parameters: This fu 1 min read Underscore.js _.extend() FunctionUnderscore.js _.extend() function is used to create a copy of all of the properties of the source objects over the destination object and return the destination object. The nested arrays or objects will be copied by using reference, not duplicated. Syntax:_.extend(destination, *sources);Parameters: 1 min read Underscore.js _.extendOwn() FunctionThe _.extendOwn() function is used to copy only own properties over to the destination object. This function is similar to the _.extend() function. Syntax: _.extendOwn(destination, *sources) Parameters: This function accept two parameters as mentioned above and described below: destination: This par 1 min read Underscore.js _.pick() FunctionUnderscore.js _.pick() function is used to return a copy of the object that is filtered using the given key. This function accepts a predicate indicating which keys are picked from the object. Syntax:_.pick(object, *keys);Parameters: object: This parameter holds the value of an object.keys: It is an 1 min read Underscore.js _.omit() FunctionUnderscore.js _.omit() function is used to return a copy of the object that was filtered to omit the blacklisted keys. Syntax:_.omit(object, *keys);Parameters:object: This parameter holds the value of an object.keys: It is an optional parameter. It contains the key name that the value needs to be om 1 min read Underscore.js _.defaults() FunctionUnderscore.js _.defaults() function returns the object after filling in its undefined properties with the first value present in the following list of default objects. Syntax:_.defaults(object, *defaults);Parameters:object: This parameter holds the value of an object.defaults: It is an optional para 1 min read Underscore.js Objects Complete ReferenceUnderscore provides a number of common object related methods such as the key and values functions. These functions are used to return the list of all keys and value of the given object respectively . All the underscore objects examples are given below with their references: Function Description _.k 3 min read Underscore.js Utility FunctionsUnderscore.js _.noConflict() FunctionUnderscore.js is a library in javascript that makes operations on arrays, string, objects much easier and handy. The _.noConflict() function is used to create a reference of the global underscore object "_" to another variable. Note: It is very necessary to link the underscore CDN before going and u 2 min read Underscore.js _.identity() FunctionUnderscore.js is a JavaScript library that makes operations on arrays, string, objects much easier and handy. The _.identity() function is used to return the exact same replica of the value given to it as the argument. This function looks useless, but is used throughout Underscore as a default itera 2 min read Underscore.js _.constant() FunctionUnderscore.js is a JavaScript library that makes operations on arrays, string, objects much easier and handy. The _.constant() function is used to create a function that returns the parameter given to it. It is almost same as _.identity() function. Note: It is very necessary to link the underscore C 2 min read Underscore.js _.noop() FunctionUnderscore.js is a JavaScript library that makes operations on arrays, string, objects much easier and handy. The _.noop() function is used to return "undefined" irrespective of the arguments passed to it. Note: It is very necessary to link the underscore CDN before going and using underscore functi 2 min read Underscore.js _.times() FunctionUnderscore.js _.times() function is used to call the function a particular number of times i.e. execution of a function(f) "n" times. NOTE: It is very necessary to link the underscore CDN before going and using the underscore functions in the browser. When linking the underscore.js CDN link, the "_" 2 min read Underscore.js _.random() FunctionUnderscore.js is a library in javascript that makes operations on arrays, string, objects much easier and handy. The _.random() function is used to return a random integer which is in the range provided to the function. Note: It is very necessary to link the underscore CDN before going and using und 2 min read Underscore.js _.mixin() FunctionUnderscore.js is a JavaScript library that makes operations on arrays, string, objects much easier and handy. The _.mixin() function is used to add extra functionality and extend the global underscore object to some special utility functions. Its important to link the underscore CDN before going and 2 min read Underscore.js _.iteratee() FunctionUnderscore.js is a JavaScript library that provides a lot of useful functions that help in the programming in a big way like the map, filter, invoke, etc. even without using any built-in objects. The _.iteratee() function is an inbuilt function in Underscore which is used to generate a callback that 2 min read Underscore.js _.uniqueId() FunctionUnderscore.js is a JavaScript library that provides a lot of useful functions that help in the programming in a big way like the map, filter, invoke, etc even without using any built-in objects. The _.uniqueId() function is an inbuilt function in Underscore.js library of JavaScript which is used to 2 min read Underscore.js _.escape() functionUnderscore.js _.escape() function is used to escape a special character string from inserting into HTML. Some of the strings that get escape are "&", ">", "<", "''", etc. Note: Some special files are needed to be included while using this code directly in the browser. It is very necessary 1 min read Underscore.js _.unescape() functionUnderscore.js is a library in javascript that makes operations on arrays, string, objects much easier and handy. _.unescape() function is used to unescape a special character. It works opposite of _.escape() function. It do convert "&" to "&" and so on. Syntax: _.unescape(string); Parame 1 min read Underscore.js _.result() FunctionUnderscore.js is a JavaScript library that provides a lot of useful functions that helps in the programming in a big way like the map, filter, invoke etc even without using any built-in objects. The _.result() function is an inbuilt function in Underscore.js library of JavaScript. Here, if the state 2 min read Underscore.js _.now() functionUnderscore.js is a library in javascript that makes operations on arrays, string, objects much easier and handy. _.now() function is used to return the timestamp of the current time. This method can be useful when working with animations in the browser. Syntax: _.now(); Parameters: It takes no param 1 min read Underscore.js Utility Complete ReferenceUnderscore.js is a lightweight JavaScript library and not a complete framework that was written by Jeremy Ashkenas that provides utility functions for a variety of use cases in our day-to-day common programming tasks. The complete reference for underscore utility is provided below: Function Descript 2 min read Underscore.js QuestionsHow to use if statements in Underscore.js templates ?Underscore.js is a javascript library that provides helper functions in functional programming without extending the built-in objects. This package consists of 100 functions like map, filter, etc. We can install this by using npm. npm install underscore Syntax: const _ = require('underscore'); Appr 1 min read Difference between lodash and UnderscoreThe lodash and UnderScore both are utility libraries from JavaScript which helps make it easier by providing utils which makes, working with arrays, numbers, objects, and strings much easier. They provide a group of tools used for common programming operations having a strong functional programming 3 min read How to break _.each() function in Underscore.js ?It is not possible to break the _.each function. The reason is that the _.each function works similarly to the forEach function and imitates its native behavior. It does not allow us to break the loop or escape from it except by throwing an exception. However, one can use different methods like: Thr 2 min read How to use underscore.js as a template engine ?While writing layouts for web pages, it is often the case that we require certain elements of the page to have dynamic values. For such scenarios, we use templates. Templates enable us to embed logical segments inside static HTML code, which makes the page dynamic based on the values passed in the t 3 min read How to deep flatten an array in JavaScript?In this article, we will learn how to deep flatten an array in JavaScript. The flattening of an array is a process of merging a group of nested arrays present inside a given array. Deep flattening means that the array would be completely flattened. Example: Input: [1,2,3,4,5,[6,[7,8,9]]] Output: [1, 2 min read Like