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])
Call
What 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
Parameter
Description
prefix
Optional. String prepended to the counter
Return Value
Return Value
Description
*
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)
Stamp. Give it a session-unique id with box_ prefix.
Insert. Append it so it is in the document.
Lookup.getElementById finds the same element — official DOM use case.
Compatibility
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 ChromeSupported · Desktop & Mobile
Full support
Mozilla FirefoxSupported · Desktop & Mobile
Full support
Apple SafariSupported · macOS & iOS
Full support
Microsoft EdgeSupported · Chromium
Full support
Internet ExplorerNo native support · Use a polyfill
Polyfill
OperaSupported · Modern versions
Full support
Samsung InternetSupported · Android
Full support
BunSupported · JavaScript runtime
Supported
DenoSupported · JavaScript runtime
Supported
Node.jsSupported · Server runtime
Supported
Android WebViewSupported · Modern WebView
Full support
_.uniqueIdUnderscore 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.