92

I'm just starting to learn JavaScript, and stumbled over Underscore.js and jQuery. Underscore looks really cool but I wonder if jQuery does not already provide functions similar to Underscore. So, is it worthwhile to use both?

3
  • 6
    The first paragraph on their home page says - "It's the tie to go along with jQuery's tux, and Backbone.js's suspenders."
    – Anurag
    Jan 18, 2012 at 8:51
  • 12
    yes, but I think what the op is indicating with the word "complement" is that since many of the functions overlap, do the two frameworks really make sense together. For instance, the differences between $.each and _.each are subtle enough to cause anyone to get confused when trying to mix and match between them.
    – BishopZ
    May 6, 2012 at 7:40
  • 1
    If you like underscorejs, then you really should be using lodash.com
    – Neil
    Apr 28, 2014 at 13:34

4 Answers 4

78

Taken from the underscore site:

It's the tie to go along with jQuery's tux, and Backbone.js's suspenders.

Underscore is more suited to data manipulation and brings many Ruby methods to JavaScript. There is some crossover, but not enough to make them mutually exclusive.

2
68
  • jQuery will take care of most of your dom manipulation
  • backbone.js will help you organize all of your code and give your js application some structure (mvc pattern)
  • underscore.js will give you really useful low-level utility. I would have never needed this library until I really got into js apps (it's also a requirement for backbone.js)
39

Underscore provides a total of 60 functions for processing data/code. It is agreed that many of the functionalities are present in other libraries like jQuery, Prototype or script.aculo.us. For example, functions like each, map, find, filter or toArray are present in jQuery. These are in Underscore also. This is to make the library independent of jQuery.

When to use Underscore?:

Currently, it is widely used with Backbone.js to use MVC architecture for creating a one page Javascript Web app. The most significant ability of underscore.js is the ability of templating (which jQuery can not do). This library has many other useful functions, which are independent of other Javascript libraries.

Find below a list of functions provided by Underscore:

Collections: each,map,reduce,reduceRight,find,filter,reject,all,any,include,invoke,pluck,max,min,sortBy,groupBy,sortedIndex,shuffle,toArray,size

Array: first,initial,last,rest,compact,flatten,without,union,intersection,difference,uniq,zip,indexOf,lastIndexOf,range

Function: bind,bindAll,memoize,delay,defer,throttle,debounce,once,after,wrap,compose

Object: keys,values,functions,extend,defaults,clone,tap,isEqual,isEmpty,isElement,isArray,isArguments,isFunction,isString,isNumber,isBoolean,isDate,isRegExp,isNaN,isNull,isUndefined

Utitity: noConflict,identity,times,mixin,uniqueId,escape,template

4
  • 3
    hi Umesh, since _.template is only one of the 60 Underscore functions, is that really enough to make it worthwhile? Apr 28, 2012 at 4:59
  • @timpeterson, I understand this. but We are not only using _.template, there are many more functions along with that like bind,bindAll, each,map etc. We can use many functions as possible. Apr 30, 2012 at 9:31
  • 3
    but bind, each, map are in jQuery so seems redundant, right? Apr 30, 2012 at 14:22
  • 2
    Having Reduce (which jQuery won't add) probably makes it worthwhile all by itself!
    – Rob Grant
    Nov 14, 2013 at 10:51
9

If you are concerned about overlap, and do not need things like JQuery AJAX, then you might consider using just the JQuery selector engine, named Sizzle.

http://sizzlejs.com/

Note that this is not for all projects, by using Sizzle and Underscore you will lose some JQuery functionality (like AJAX), you need to consider what you really need for your specific application.

1

Not the answer you're looking for? Browse other questions tagged or ask your own question.