Underscore.js Introduction

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.
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.
| Need | How Underscore helps |
|---|---|
| Collection helpers | _.map, _.filter, _.groupBy, _.pluck |
| Two calling styles | Functional args or _(value).method() |
| Fluent pipelines | _.chain … .value() |
| Safe prototypes | Helpers stay on _, not Array.prototype |
How Underscore Calls Run
Pick a style, then pass data through helpers:
| Step | What happens |
|---|---|
| 1. Load | npm install underscore or a CDN UMD script |
| 2. Call | Functional _.map(list, fn) or OOP _(list).map(fn) |
| 3. Chain (optional) | For many steps, _.chain until .value() |
| 4. Use result | Usually 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:
npm install underscore <!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> Same result, two styles
_.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:
| Underscore | Lodash | |
|---|---|---|
| Style | Functional + OOP wrap + _.chain | Mostly functional; _.chain available |
| API size | Focused ~100+ helpers | Larger utility surface |
| Modular imports | Typically whole library | Strong per-method packages |
| Heritage | Original utility library | Grew from Underscore ideas |
When to use which
| Use Underscore when… | Prefer Lodash when… |
|---|---|
| You maintain Underscore code or like the OOP wrap | You want a larger grab-bag and modular imports |
You need _.template and classic helpers | You already standardized on Lodash |
| You want a smaller, stable API surface | You need deep clone, debounce, and path helpers often |
Core Building Blocks
These ideas show up in almost every Underscore script:
| Concept | What it does | Example |
|---|---|---|
| Functional | Pass data as an argument | _.map(list, fn) |
| OOP wrap | Call methods on _(value) | _(list).map(fn) |
_.chain | Keep wrappers for multi-step pipelines | _.chain(list).map(fn).value() |
| Collections | Work on arrays and objects alike | _.each, _.filter, _.reduce |
| No prototype pollution | Helpers stay on _ | Not on Array.prototype |
Where Is Underscore Used?
You will still meet Underscore across the JS ecosystem:
| Use case | Why Underscore fits |
|---|---|
| Legacy apps | Already wired; cheaper to extend than rewrite |
| Data shaping | _.map, _.pluck, and _.groupBy over JSON |
| Templates | _.template for simple string interpolation |
| Backbone apps | Classic pairing with Backbone.js models/views |
| Fluent transforms | _.chain pipelines for multi-step cleanup |
| Teaching utilities | Clear 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
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.
7 people found this page helpful
