Underscore.JS - Quick Guide

Underscore.JS is a popular javascript based library which provides 100+ functions to facilitate web development. It provides helper functions like map, filter, invoke as well as function binding, javascript templating, deep equality checks, creating indexes and so on. Underscore.JS can be used directly inside a browser and also with Node.js.
Working with objects using JavaScript can be quite challenging, specifically if you have lots of manipulation to be done with them. Underscore comes with lots of features that eases your work with objects.
Underscore.JS is an open source project and you can easily contribute to the library and add features in the form of plugins and make it available on GitHub and in Node.js.
Let us understand in detail all the important features available with Underscore −
Underscore.JS provides various functions for collections like each, map, reduce which are used to apply an operation on each item of a collection. It provides method like groupBy, countBy, max, min which processes collections and ease lot of tasks.
Underscore.JS provides various functions for arrays like to iterate and process arrays like first, initial, lastIndexOf, intersection, difference etc.
Underscore.JS provides functions such as bind, delay, before, after etc.
Underscore.JS provides functions to manipulate objects, to map objects and comparing objects. For example, keys, values, extends, extendsOwn, isEqual, isEmpty etc.
Underscore.JS provides various utilities methods like noConflict, random, iteratee, escape etc.
Underscore.JS provides chaining methods as well like chain, value.
In subsequent chapters, we'll cover importants functions of Underscore.JS
In this chapter, you will learn in detail about setting up the working environment of Underscore.JS on your local computer. Before you begin with working on Underscore.JS, you need to have the access to the library. You can access its files in any of the following methods −
In this method, we are going to need Underscore.JS file from its official website and will use it directly in the browser.
As a first step, go to the official website of Underscore.JS https://underscorejs.org/.
Observe that there is a download option available which gives you the latest underscore-min.js file UMD (Production) available. Right Click on the link and choose save as. Note that the file is available with and without minification.
Now, include underscore-min.js inside the script tag and start working with Underscore.JS. For this, you can use the code given below −
<script type = "text/JavaScript" src = "https://underscorejs.org/underscore-min.js"></script>
Given here is a working example and its output for a better understanding −
<html>
   <head>
      <title>Underscore.JS - Working Example</title>
      <script type = "text/JavaScript" src = "https://underscorejs.org/underscore-min.js"></script>
      <style>
         div {
            border: solid 1px #ccc;
            padding:10px;
            font-family: "Segoe UI",Arial,sans-serif;
            width: 50%;
         }
      </style>
   </head>
   <body>
      <div style = "font-size:25px" id = "list">
	  </div>
      <script type = "text/JavaScript">
         var numbers = [1, 2, 3, 4];
         var listOfNumbers = '';
         _.each(numbers, function(x) { listOfNumbers += x + ' ' });
         document.getElementById("list").innerHTML = listOfNumbers;
      </script>
   </body>
</html>
If you are opting for this method, make sure you have Node.js and npm installed on your system. You can use the following command to install Underscore.JS −
npm install underscore
You can observe the following output once Underscore.JS is successfully installed −
+ underscore@1.10.2
added 1 package from 1 contributor and audited 1 package in 6.331s
found 0 vulnerabilities

Now, to test if Underscore.JS works fine with Node.js, create the file tester.js and add the following code to it −
var _ = require('underscore');
var numbers = [1, 2, 3, 4];
var listOfNumbers = '';
_.each(numbers, function(x) { listOfNumbers += x + ' ' });
console.log(listOfNumbers);
Save the above program in tester.js. The following commands are used to compile and execute this program.
\>node tester.js

1 2 3 4

Underscore.JS has many easy to use methods which helps in iterating Collections. This chapter discusses them in detail.
Underscore.JS provides various methods to iterate the Collections as listed below −
_.each(list, iteratee, [context])
_.map(list, iteratee, [context])
_.reduce(list, iteratee, [memo], [context])
_.reduceRight(list, iteratee, [memo], [context])
_.find(list, predicate, [context])
_.filter(list, predicate, [context])
_.where(list, properties)
_.findWhere(list, properties)
_.reject(list, predicate, [context])
_.every(list, [predicate], [context])
_.some(list, [predicate], [context])
Underscore.JS has many easy to use methods which helps in processing Collections. This chapter discusses them in detail.
Underscore.JS provides various methods to process the Collections as listed below −
_.contains(list, value, [fromIndex])
_.invoke(list, methodName, *arguments)
_.pluck(list, propertyName)
_.max(list, [iteratee], [context])
_.min(list, [iteratee], [context])
_.sortBy(list, iteratee, [context])
_.groupBy(list, iteratee, [context])
_.indexBy(list, iteratee, [context])
_.countBy(list, iteratee, [context])
_.shuffle(list)
_.sample(list, [n])
_.toArray(list)
_.size(list)
_.partition(list, predicate)
_.compact(list)
Underscore.JS has many easy to use methods which helps in iterating Arrays. This chapter discusses them in detail.
Underscore.JS provides various methods to iterate the Arrays as listed below −
_.first(array, [n])
_.initial(array, [n])
_.last(array, [n])
_.rest(array, [index]) 
_.indexOf(array, value, [isSorted])
_.lastIndexOf(array, value, [fromIndex])
_.sortedIndex(array, value, [iteratee], [context])
_.findIndex(array, predicate, [context])
_.findLastIndex(array, predicate, [context])
Underscore.JS has many easy to use methods which helps in processing Arrays. This chapter discusses them in detail.
Underscore.JS provides various methods to process the Arrays as listed below −
_.flatten(array, [shallow])
_.without(array, *values)
_.union(*arrays)
_.intersection(*arrays)
_.difference(array, *others)
_.uniq(array, [isSorted], [iteratee])
_.zip(*arrays)
_.unzip(array)
_.object(list, [values])
_.chunk(array, length)
_.range([start], stop, [step])
Underscore.JS has many easy to use methods which helps in handling functions. This chapter discusses them in detail.
Underscore.JS provides various methods to handle functions as listed below −
_.bind(function, object, *arguments)
_.partial(function, *arguments)
_.memoize(function, [hashFunction])
_.delay(function, wait, *arguments)
_.once(function)
_.before(count, function)
_.wrap(function, wrapper)
_.negate(predicate)
_.compose(*functions)
Underscore.JS has many easy to use methods which helps in mapping objects. This chapter discusses them in detail.
Underscore.JS provides various methods to handle object mapping as listed below −
_.keys(object)
_.allKeys(object)
_.values(object)
_.mapObject(object, iteratee, [context])
_.pairs(object)
_.invert(object)
_.create(prototype, props)
_.functions(object)
_.findKey(object, predicate, [context])
Underscore.JS has many easy to use methods which helps in updating objects. This chapter discusses them in detail.
Underscore.JS provides various methods to handle object updates as listed below −
_.extend(destination, *sources)
_.pick(object, *keys)
_.omit(object, *keys)
_.defaults(object, *defaults)
_.clone(object)
_.tap(object, interceptor)
_.has(object, key)
_.property(path)
_.propertyOf(object)
Underscore.JS has many easy to use methods which helps in comparing objects. This chapter discusses them in detail.
Underscore.JS provides various methods to handle object comparison as listed below −
_.matcher(attrs)
_.isEqual(object, other)
_.isMatch(object, properties)
_.isEmpty(object)
_.isArray(object)
_.isObject(value)
_.isArguments(object)
_.isFunction(object)
_.isString(object)
_.isNumber(object)
_.isFinite(object)
_.isBoolean(object)
_.isDate(object)
_.isRegExp(object)
_.isError(object)
_.isSymbol(object)
_.isMap(object)
_.isWeakMap(object)
_.isSet(object)
_.isWeakSet(object)
_.isNaN(object)
_.isNull(object)
_.isUndefined(value)
Underscore.JS has many easy to use utility methods. This chapter discusses them in detail.
Underscore.JS provides various utility methods as listed below −
_.identity(value)
_.constant(value)
_.noop()
_.times(n, iteratee, [context])
_.random(min, max)
_.mixin(object)
_.iteratee(value, [context])
_.uniqueId([prefix])
_.escape(string)
_.unescape(string)
_.result(object, property, [defaultValue])
_.now()
_.template(templateString, [settings])
Underscore.JS has methods to create a chain of methods and then retrive their effective result. This chapter discusses them in detail.
Underscore.JS provides various utility methods as listed below −
_.chain(object)
_.chain(obj).value()
 Print
 Add Notes
 Bookmark this page