Skip to content

Guide · Open source

How to use Underscore.js: install, core helpers and examples

Updated · written with AI from the project's own documentation and checked against our catalogue data

Underscore.js is a utility library for JavaScript. It provides common functional helpers such as each, map, reduce and filter, and according to its README it does this without extending any core JavaScript objects. This guide explains what it is, how to install it, and how to use its helpers on arrays and objects so you can judge whether it fits your project.

Underscorejashkenas/underscore · ★ 27.3k · MIT

Quick start

  1. 1Check your environment. Confirm you have Node.js or a browser environment available.
  2. 2Get the library. Clone the repository with the git command shown in the README, or follow the package-manager route described at underscorejs.org.
  3. 3Read the docs. Open https://underscorejs.org to see the full list of helpers and their signatures.
  4. 4Pick a helper. Start with each, map, reduce or filter on an array you already have.
  5. 5Replace a hand-written loop. Rewrite one loop that transforms data using a helper and compare readability.
  6. 6Check built-ins first. Confirm whether modern built-in array methods already cover your case before adding a dependency.

What is Underscore.js and what does it do?

Underscore.js is described as a utility-belt library for JavaScript. It supplies the usual functional helpers, including each, map, reduce and filter, which are used to work with collections such as arrays and objects.

The README says the library does not extend any core JavaScript objects. Your built-in types stay as they are, and you call Underscore's functions explicitly.

Install Underscore.js with a package manager

The project lists a package-manager install method. The README's install instruction is to clone the repository, shown below. For Docs, pre-packed downloads and tests, the README points to underscorejs.org.

Underscore runs in Node.js or in a browser environment, so you need one of those.

  • Prerequisite: Node.js or a browser environment
  • Documentation and pre-packed downloads: https://underscorejs.org
git clone https://github.com/jashkenas/underscore.git

Use each, map, reduce and filter on collections

The core idea is to pass a collection and a function to a helper. Underscore calls that function for each item and returns the result. Check the official docs at underscorejs.org for the exact signatures of every helper before you rely on one.

Helpers like these replace hand-written loops, which tends to make transformation code shorter and easier to read.

  • each: run a function on every item
  • map: build a new collection from transformed items
  • reduce: combine a collection into a single value
  • filter: keep only the items that pass a test

Choose Underscore for legacy code or a functional style

Underscore suits developers who want common collection helpers without writing them by hand. It also suits maintainers of older codebases that need utility functions without changing built-in objects, and teams that prefer a functional style for transforming data.

The README does not compare Underscore with modern built-in array methods. Many of those methods now cover the same ground, so check whether the built-ins already do what you need.

Compare Underscore with Lodash and Ramda

Comparable projects named in the source are Lodash and Ramda. The source does not benchmark them or rank them against Underscore, so evaluate each against your own code.

Underscore is not a full application framework. The README describes only a utility library.

What people use it for

  • Transforming arrays of records with map and filter in a data pipeline
  • Summing or combining collection values with reduce
  • Maintaining an older JavaScript codebase that needs utility helpers without changing built-in objects
  • Writing iteration logic in a functional style over objects and arrays

Frequently asked questions

Is Underscore.js free to use?

The source lists the licence as MIT. Check the LICENSE file in the repository for the full terms before you use it in a project.

What do I need to run Underscore.js?

The README lists Node.js or a browser environment as the prerequisite. Underscore is a library, so you use it inside your JavaScript code.

How do I install Underscore.js?

The README's listed install command is to clone the repository with git. The project also points to underscorejs.org for docs and pre-packed downloads.

Does Underscore.js change built-in JavaScript objects?

No. According to the README, Underscore does not extend any core JavaScript objects.

Underscore vs Lodash: which should I use?

The source names Lodash and Ramda as comparable projects but does not compare them. Try the helpers you need in each and pick the one that fits your code.

Can I use Underscore instead of modern array methods?

You can, but many modern built-in array methods now cover the same ground. If built-ins do what you need, you may not need Underscore.

New agent skills, repos and AI tools — every week

The best new skills and open-source projects, the fastest-growing repos, and one free prompt. Five minutes to read. Unsubscribe anytime.

Our verdict

Underscore.js suits JavaScript developers who want common collection helpers, and maintainers of older codebases that must not change built-in objects. If your project only needs modern built-in array methods, or you want a full application framework, pick something else.

Install options & downloads