1. javascript
  2. /libraries
  3. /underscorejs

Underscore.js - A Versatile JavaScript Library

What is Underscore.js?

Underscore.js was developed in 2009 and has been widely used since then. As JavaScript evolved, many of the features are now either included in the language itself or available in other libraries. The introduction of ECMAScript 6 (ES6) brought many new features, such as arrow functions, template literals, and the spread operator, which provide similar functionality.

With the rise of functional programming libraries like Lodash and Ramda, Underscore.js may not be as widely used as it once was. These libraries provide a similar set of features and have a more robust set of functional programming helpers.

Why use Underscore.js?

Using Underscore.js is largely dependent on the specific needs of your project. Here are a few use cases you should consider:

  • If you are working on a project that was built using Underscore.js. It may be easier to continue using the library instead of switching to a different library or rewriting existing code.

  • Your project has specific requirements that are not met by other libraries: Underscore.js has been around for over a decade and provides a wide range of functional programming helpers and utility functions that may not be available in other libraries.

  • You want to keep the library dependencies at a minimum: Underscore.js is a lightweight library that has a small footprint.

Basic Setup

You'll first need to include the library in your HTML file. This can be done by downloading it from the official website and including it in your project's file structure.

<script src="underscore.js"></script>

Once you've included the library, you can start using it in your JavaScript code. Here's an example of how to use the each function to iterate over an array of numbers:

var numbers = [1, 2, 3, 4, 5];

_.each(numbers, function(num) {
  console.log(num);
});

Alternatively, you can also include underscore.js via a package manager like npm or yarn.

npm install underscore
yarn add underscore

Key Underscore.js Functions and Features

Here are some examples of some of the key features provided by Underscore.js:

  • Functional programming helpers: each, map, reduce, filter, find, findWhere, reject, every, some, contains, invoke, pluck, max, min, sortBy, groupBy, countBy, shuffle, toArray, size, partition.

  • Utility functions: extend, defaults, clone, tap, has, matches, isEqual, isEmpty, isElement, isArray, isObject, isArguments, isFunction, isString, isNumber, isFinite, isBoolean, isDate, isRegExp, isError, isNaN, isNull, isUndefined.

  • Object functions: keys, values, pairs, invert, functions, extendOwn, pick, omit.

  • Chaining functions: chain, value, mixin.

  • Template functions: template.

Keep in mind that the map, filter, and reduce functions are now available in the built-in Array prototype, making it unnecessary to use underscore.js for these operations. Similarly, the Object.assign method can be used instead of extend and defaults functions.

Summary

Underscore.js is still widely used in many projects and has a large enough community. It's important to consider the specific needs of your project and weigh the pros and cons before commiting to it. In some cases, it may may still be the best choice as it provides many useful functional programming helpers, utility functions and its API design is simple and easy to use.