Using The Underscore.js Library

Joe Cha
bother7-blog
Published in
4 min readNov 27, 2017

I had a live coding interview recently, and there was this line of code at the top of the file that I didn’t really understand, so I deleted it.

var _ = require('underscore')

It was silly that my first instinct was to delete a line of code because I had never seen it before. Turns out, the place interviewing me was helping me out by including the underscore library for my javascript code. Embarrassingly, I had to ask the interviewer to place the deleted code back into the file. I’m glad they did because underscore is a powerful library filled with functions that supplement the base javascript library functions.

Underscore is a javascript library filled with useful function for manipulating data. Data can be stored in arrays, objects, functions, and underscore also provides other functions as well. There are functions we are already used to seeing in javascript, like map, reduce, and filter, but there are a bunch of other functions that are part of other programming languages, but not part of the base javascript library. One example is pluck.

A slide from Jitendra Zaa’s Introduction to Underscore.js

I highly suggest you visit the Underscore website to see documentation on every function the library provides. It is what I did during my interview, and there were two functions I used specifically that would have required more legwork if I didn’t use underscore. These functions were .shuffle and .random.

From the underscore documentation
From the underscore documentation

The documentation was clear, and the functions themselves were intuitive and easy to implement. Random would have required writing out a function using the javascript Math library and I don’t even know how I would write a shuffle function but it most likely would have used the Math library as well. These functions were great to use, but it was interesting to see that underscore has functions javascript already possesses, like map.

From the underscore documentation

Why would we need to use this map function over the one already built into javascript? I guess the key difference is that the array is being passed into the function, as opposed to being an object-oriented function like the base library version. The array is an input instead of being the object which the function is being implemented on. I guess this could potentially be a space saver, as the array doesn’t need to be declared beforehand.

Underscore is a super useful library, so why have I never encountered it before? It sounds like lodash is very similar to underscore, and actually possesses more functions than underscore. Both are very well maintained, with recent updates on github. I have seen lodash once or twice, but I believe I only used it once. Lodash and underscore were never really taught to us in bootcamp. Why would Flatiron School not want to use lodash and underscore? I guess the way we learned to code in bootcamp, it was better to use the vanilla base version of javascript. If we started to use underscore and started depending on it, then we wouldn’t really be able to differentiate what underscore does from base javascript. If we opened up a javascript console to mess around in, we wouldn’t really understand why the _ library wasn’t there. I guess by learning not to use it, we can appreciate what the library does and how we need to connect to implement it. There will be occasions that arise where the underscore library won’t be available, and we should be able to understand why it’s not there.

References:

http://underscorejs.org/#contains

https://www.slideshare.net/jitendrazaa/introduction-to-underscorejs-40846429

--

--