Underscore.js _.uniqueId() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Utility

Definition

Generate a globally unique id for client-side models or DOM elements that need one. If prefix is passed, the id is appended to it (official Underscore docs). Stable utility — not deprecated.

Pass the required arguments shown below. Underscore utility helpers usually return a new value or function — check edge cases before you ship.

Syntax

Call _.uniqueId. Start with this line.

Underscore
_.uniqueId([prefix])
CallWhat it does
_.uniqueId([prefix])Primary signature.
_.uniqueId(...)Call with the arguments your case needs.

Load Underscore with import _ from "underscore" (or the UMD CDN build) so these helpers are available as _.

Library declarations
Underscore
_.uniqueId([prefix])

Parameters

ParameterDescription
prefixOptional. String prepended to the counter

Return Value

Return ValueDescription
*Always a string

Underscore.js Examples

Example 1 — Official contact_ Prefix

Prepend a prefix to the session counter — the docs sample.

Underscore
_.uniqueId("contact_");
// → "contact_1"  (fresh page)
// → "contact_104" after many prior calls (docs sample)
Try It Yourself

How It Works

  1. Prefix. "contact_" is prepended to the counter.
  2. Bump. Underscore increments the shared session counter.
  3. Fresh page. The first call is usually "contact_1".
  4. Docs note. The official page shows 104 because that page had already used the counter.

Example 2 — No Prefix (Always a String)

Omit the prefix to get "1", then "2". The type is always "string".

Underscore
var a = _.uniqueId();
_.uniqueId();
typeof a;
// → "1"
// → "2"
// → "string"
Try It Yourself

How It Works

  1. First call. Counter becomes 1 → string "1".
  2. Second call. Counter becomes 2 → "2".
  3. Type. Underscore concatenates with '', so it is never a number.
  4. Compare carefully. "1" !== 1.

Example 3 — One Shared Counter

Different prefixes still share the same counter.

Underscore
_.uniqueId("contact_");  // → "contact_1"
_.uniqueId("user_");     // → "user_2"
_.uniqueId("contact_");  // → "contact_3"
Try It Yourself

How It Works

  1. First. contact_ + 1.
  2. Second. Prefix changes, but the counter continues → user_2.
  3. Third. Back to contact_ → contact_3, not contact_2.
  4. Rule. Prefixes label ids; they do not own separate counters.

Example 4 — Several Ids with _.times

Pair with _.times to mint a short list of prefixed ids.

Underscore
_.times(3, function () {
  return _.uniqueId("n_");
});
// → ["n_1", "n_2", "n_3"]
Try It Yourself

How It Works

  1. Loop. _.times(3, …) runs the iteratee three times.
  2. Each call. _.uniqueId("n_") bumps the counter once.
  3. Collect. Results land in an array in order.
  4. Fresh page. Starts at n_1 unless earlier examples already ran.

Example 5 — Temporary DOM Element Id

Assign a unique id so getElementById can find the node.

Underscore
var el = document.createElement("div");
el.id = _.uniqueId("box_");
document.body.appendChild(el);

el.id;
// → "box_1"

document.getElementById(el.id) === el;
// → true
Try It Yourself

How It Works

  1. Create. Build a new div in memory.
  2. Stamp. Give it a session-unique id with box_ prefix.
  3. Insert. Append it so it is in the document.
  4. Lookup. getElementById finds the same element — official DOM use case.

Browser & Runtime Support

_.uniqueId ships with Underscore.js (1.x). It runs anywhere Underscore runs — modern browsers (CDN or bundle) and Node.js / Bun / Deno with npm install underscore.

✓ Stable · Underscore 1.x

_.uniqueId()

Available in [email protected] (UMD / ESM). Session-local counter for client-side models and temporary DOM ids. Works in evergreen browsers and current Node LTS when Underscore is loaded.

100% With Underscore 1.x
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full support
_.uniqueId Underscore 1.x+

Bottom line: Safe for production once Underscore is on the page or in your bundle. Not a native browser API — load Underscore first. Prefer a pinned CDN version (e.g. [email protected]).

Frequently Asked Questions

_.uniqueId([prefix]) returns a string id that is unique within the current JavaScript session. Underscore bumps one shared counter and concatenates it to an optional prefix. The official example is _.uniqueId('contact_').
_.uniqueId('contact_') returns a string such as 'contact_104' after many prior calls. On a fresh page the first call is 'contact_1'. The docs sample is 104 because that page had already used the counter.
Always a string. Underscore converts the counter with + '' so typeof _.uniqueId() is 'string'. That is why '1' !== 1.
No. There is one counter for the whole Underscore object. After _.uniqueId('contact_') returns 'contact_1', the next _.uniqueId('user_') is 'user_2', not 'user_1'.
No. It is a session-local incrementing label for client-side models or temporary DOM ids. The counter resets on reload. Use crypto.randomUUID(), a database id, or another server-generated key for persisted or guess-resistant values.
No. It is a stable Underscore utility. The source describes it as unique within the entire client session — useful for temporary DOM ids.

Next: _.escape()

HTML-escape a string so it can be inserted into markup safely.

_.escape() tutorial →

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.

11 people found this page helpful