Underscore.js Utility Methods

About This Index
This page is the Underscore.js Utility index for the tutorial track. Each row links to a full guide with syntax, examples, and FAQs.
Learn objects and collections on /underscore-js/object and /underscore-js/collection. Use this page when you need everyday glue such as _.identity(), _.times(), or _.template().
Start with _.identity(), then _.times(), _.escape(), and _.template()—then browse the rest by category.
How to Call a Utility Method
Call utilities on _ like any other Underscore helper:
_.identity(5); // 5
_.constant("ok")(); // "ok"
_.noop(); // undefined
_.times(3, function (i) { return i * 2; }); // [0, 2, 4]
_.random(1, 6); // e.g. 4
_.uniqueId("user_"); // "user_1"
_.escape("<div>"); // "<div>"
const greet = _.template("Hello <%= name %>!");
greet({ name: "moe" }); // "Hello moe!"
_.VERSION; // e.g. "1.13.7" Underscore.js Utility Methods Table
All utility method guides in this Underscore.js tutorial series (15 tutorials).
Getting Started
4 methodsReclaim _, identity defaults, constants, and no-op callbacks.
| Method | Description | Tutorial |
|---|---|---|
_.noConflict() | Gives the global _ variable back to its previous owner and returns Underscore. | Try it |
_.identity() | Returns the same value that was passed in — the default iteratee. | Try it |
_.constant() | Creates a function that always returns the same value. | Try it |
_.noop() | A function that does nothing — useful as a default callback. | Try it |
Generate
3 methodsRepeat work, pick random integers, and mint unique ids.
| Method | Description | Tutorial |
|---|---|---|
_.times() | Invokes the iteratee n times and returns an array of results. | Try it |
_.random() | Returns a random integer between min and max, inclusive. | Try it |
_.uniqueId() | Generates a globally unique id, optionally with a prefix. | Try it |
Extend Underscore
2 methodsAdd custom methods and normalize iteratees.
| Method | Description | Tutorial |
|---|---|---|
_.mixin() | Adds your own functions to Underscore and the OOP wrapper. | Try it |
_.iteratee() | Generates a callback from a function, property name, or matcher object. | Try it |
Strings & Values
4 methodsEscape HTML, resolve properties, and read the current time.
| Method | Description | Tutorial |
|---|---|---|
_.escape() | Escapes HTML special characters for safe insertion into markup. | Try it |
_.unescape() | The inverse of _.escape — turns HTML entities back into characters. | Try it |
_.result() | Reads a property; if it is a function, calls it with the object as this. | Try it |
_.now() | Returns an integer timestamp for the current time. | Try it |
Template & Version
2 methodsCompile HTML templates and read the loaded library version.
| Method | Description | Tutorial |
|---|---|---|
_.template() | Compiles a JavaScript template into a render function. | Try it |
_.VERSION | The current Underscore version string (property, not a function). | Try it |
Frequently Asked Questions
Start with _.identity()
Open the identity guide—syntax, examples, and FAQs.
12 people found this page helpful
