Underscore.js Introduction

Beginner
6 min read
Updated: Oct 2026
JavaScript utility library

What Is Underscore.js?

Underscore is a JavaScript utility library with 100+ helpers for collections, arrays, objects, functions, and templates. It lives on the _ object and does not extend built-in prototypes.

Remember
Convention: import as _
  var _ = require('underscore')
  import _ from 'underscore'

Two styles (same result):
  _.map(list, fn)     // functional
  _(list).map(fn)     // object-oriented wrap

Multi-step pipelines:
  _.chain(list).map(fn).filter(pred).value()

These tutorials use [email protected]. Lodash grew out of Underscore; Underscore remains a stable library with its own wrapper and chaining API.

Why Use Underscore?

Native map / filter cover a lot today. Underscore still helps when you want a consistent toolkit—including older browsers, templates, and fluent chains.

NeedHow Underscore helps
Collection helpers_.map, _.filter, _.groupBy, _.pluck
Two calling stylesFunctional args or _(value).method()
Fluent pipelines_.chain … .value()
Safe prototypesHelpers stay on _, not Array.prototype

How Underscore Calls Run

Pick a style, then pass data through helpers:

StepWhat happens
1. Loadnpm install underscore or a CDN UMD script
2. CallFunctional _.map(list, fn) or OOP _(list).map(fn)
3. Chain (optional)For many steps, _.chain until .value()
4. Use resultUsually a plain array/object—not a wrapper

Your First Underscore Utility

Install via npm, or load from a CDN and double a list with _.map:

terminal
npm install underscore
HTML
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Underscore CDN demo</title>
</head>
<body>
  <h1>Underscore.js in the browser</h1>
  <p>Doubled list:</p>
  <pre id="out"></pre>
  <script src="https://cdn.jsdelivr.net/npm/[email protected]/underscore-umd-min.js"></script>
  <script>
    var doubled = _.map([1, 2, 3], function (n) {
      return n * 2;
    });
    document.getElementById("out").textContent = JSON.stringify(doubled);
  </script>
</body>
</html>
Try It Yourself

Same result, two styles

JavaScript
_.map([1, 2, 3], function (n) { return n * 2; });
_([1, 2, 3]).map(function (n) { return n * 2; });
// Both → [2, 4, 6]

How It Works

1. Load. The CDN (or require) exposes _.

2. Transform. _.map(list, fn) walks each item and returns a new array.

3. Optional wrap. _(list).map(fn) is the OOP form of the same call.

Next, explore collection helpers in Collections.

Underscore vs Lodash

Lodash started as an Underscore fork—know how they differ today:

UnderscoreLodash
StyleFunctional + OOP wrap + _.chainMostly functional; _.chain available
API sizeFocused ~100+ helpersLarger utility surface
Modular importsTypically whole libraryStrong per-method packages
HeritageOriginal utility libraryGrew from Underscore ideas

When to use which

Use Underscore when…Prefer Lodash when…
You maintain Underscore code or like the OOP wrapYou want a larger grab-bag and modular imports
You need _.template and classic helpersYou already standardized on Lodash
You want a smaller, stable API surfaceYou need deep clone, debounce, and path helpers often

Core Building Blocks

These ideas show up in almost every Underscore script:

ConceptWhat it doesExample
FunctionalPass data as an argument_.map(list, fn)
OOP wrapCall methods on _(value)_(list).map(fn)
_.chainKeep wrappers for multi-step pipelines_.chain(list).map(fn).value()
CollectionsWork on arrays and objects alike_.each, _.filter, _.reduce
No prototype pollutionHelpers stay on _Not on Array.prototype

Where Is Underscore Used?

You will still meet Underscore across the JS ecosystem:

Use caseWhy Underscore fits
Legacy appsAlready wired; cheaper to extend than rewrite
Data shaping_.map, _.pluck, and _.groupBy over JSON
Templates_.template for simple string interpolation
Backbone appsClassic pairing with Backbone.js models/views
Fluent transforms_.chain pipelines for multi-step cleanup
Teaching utilitiesClear functional vs OOP calling styles

Key Takeaways

  • Underscore is a utility library on _—not native prototypes.
  • Two styles: _.map(list, fn) and _(list).map(fn).
  • Multi-step: use _.chain … .value().
  • Still maintained—Lodash is related, not a replacement for every app.

One line: Underscore shortens collection work with _—next, learn Collections.

Frequently Asked Questions

Underscore is a JavaScript utility library with more than 100 functional helpers for collections, arrays, objects, functions, and templates. It does not extend built-in objects. The current docs version used in these tutorials is 1.13.8.
You can wrap a value with _(list) and call methods on the wrapper, for example _([1, 2, 3]).map(fn). That is the same result as the functional form _.map([1, 2, 3], fn). The official Object-Oriented Style section shows both lines doubling a list.
No. Object-oriented style wraps once; a single method such as .map() usually returns a plain array. _.chain keeps returning wrappers until you call .value() to unwrap.
In Node or a bundler run npm install underscore, then var _ = require('underscore') or import _ from 'underscore'. In the browser, load the UMD build from a CDN, for example underscore-umd-min.js version 1.13.8.
No. Underscore 1.x is stable and still documented on underscorejs.org. Lodash grew out of Underscore, but Underscore remains a supported library with its own API, chaining, and object-oriented wrapper.
Open the Collections hub and start with _.each, then _.map, _.filter, and _.reduce. When you want fluent pipelines, continue to _.chain and .value().

Did you know?

You can call helpers functionally as _.map(list, fn) or wrap the value in object-oriented style as _(list).map(fn). The official docs treat those two doubling examples as identical. These tutorials use [email protected].

Next: Collections

Practice the most-used Underscore helpers, starting with collections.

Collections →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

7 people found this page helpful