Lodash _.uniqueId() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Util

Definition

Generates a unique ID. If prefix is given, the ID is appended to it. Part of Lodash Util since 0.1.0 (stable — not deprecated).

Note: Uses one shared counter for the whole Lodash instance. IDs are unique in the current runtime only — not across reloads or servers.

Syntax

Call _.uniqueId. Start with this line.

Lodash
_.uniqueId([prefix=''])
CallWhat it does
_.uniqueId([prefix=''])Primary signature.
_.uniqueId(...)Call the util helper.

Prefer import uniqueId from "lodash/uniqueId" when you want a smaller bundle.

Library declarations
Lodash
_.uniqueId([prefix=''])

Parameters

ParameterDescription
prefixOptional. String prefixed before the number (default '')

Return Value

Return ValueDescription
*Unique ID as a string

Lodash Examples

Example 1 — Official: Prefix then Bare

Prefix the first ID, then generate a bare numeric ID.

Lodash
console.log(_.uniqueId('contact_'));
// → 'contact_1'

console.log(_.uniqueId());
// → '2'
Try It Yourself

How It Works

  1. Prefix. 'contact_' is placed before the number.
  2. First call. Counter becomes 1 → 'contact_1'.
  3. Second call. No prefix; counter becomes 2 → '2'.
  4. Docs note. Official playgrounds may show higher numbers if IDs were already used.

Example 2 — Bare Incrementing IDs

Omit the prefix for simple numeric strings.

Lodash
console.log(_.uniqueId()); // '1'
console.log(_.uniqueId()); // '2'
console.log(_.uniqueId()); // '3'
Try It Yourself

How It Works

  1. Empty prefix. Default prefix is ''.
  2. Increment. Each call adds one to the shared counter.
  3. String. Results are '1', '2', '3' — not numbers.
  4. Order. Sequence follows call order in this runtime.

Example 3 — Same Prefix Twice

Reuse one label prefix for related UI elements.

Lodash
console.log(_.uniqueId('user_')); // 'user_1'
console.log(_.uniqueId('user_')); // 'user_2'
Try It Yourself

How It Works

  1. Label. 'user_' makes IDs easy to spot in the DOM.
  2. First. Counter 1 → 'user_1'.
  3. Second. Counter 2 → 'user_2'.
  4. Use. Handy for temporary element id attributes.

Example 4 — Shared Counter Across Prefixes

Different prefixes still share one counter.

Lodash
console.log(_.uniqueId('a_')); // 'a_1'
console.log(_.uniqueId('b_')); // 'b_2'  (not b_1)
console.log(_.uniqueId('a_')); // 'a_3'
Try It Yourself

How It Works

  1. One counter. Prefix does not reset the number.
  2. a_1. First call in this runtime.
  3. b_2. Next number is 2, even with a new prefix.
  4. a_3. Back to a_ still continues the sequence.

Example 5 — Generate Several with _.times

Build an array of unique IDs in one expression.

Lodash
var ids = _.times(3, function () {
  return _.uniqueId('row_');
});

console.log(ids);
// → ['row_1', 'row_2', 'row_3']
Try It Yourself

How It Works

  1. times. Runs the iteratee three times.
  2. Each run. Calls _.uniqueId('row_').
  3. Collect. Returns an array of three strings.
  4. Note. Pass a function — not _.uniqueId alone — so the prefix is applied each time.

Browser & Runtime Support

_.uniqueId is a Lodash Util helper since 0.1.0. It runs anywhere Lodash runs — modern browsers (CDN or bundle) and Node.js / Bun / Deno with npm install lodash.

✓ Stable · Lodash 0.1.0+

_.uniqueId()

Available in [email protected] full builds and the lodash/uniqueId modular import. Works in evergreen browsers and current Node LTS when Lodash is installed.

100% With Lodash 0.1+
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 Lodash 0.1.0+

Bottom line: Safe for production once Lodash is loaded. Prefer import uniqueId from "lodash/uniqueId" for smaller bundles. Use it for temporary local IDs; use UUID APIs for global uniqueness.

Frequently Asked Questions

_.uniqueId([prefix]) generates a unique string ID. If prefix is given, the incrementing number is appended to it. Official style: _.uniqueId('contact_') → 'contact_1' on a fresh counter.
No. With no arguments you get "1", "2", "3", … as strings. With a prefix like "user_" you get "user_1", "user_2", and so on.
No. Lodash uses one shared counter for all _.uniqueId calls in the process. After "a_1", the next _.uniqueId("b_") is "b_2", not "b_1".
The counter resets when the runtime reloads. IDs are unique only within the current JavaScript process—not across tabs, servers, or sessions.
No. It is for lightweight in-memory or DOM labels. Prefer database IDs, UUID, or another server-generated value for persisted records.
Use import uniqueId from "lodash/uniqueId" or require("lodash/uniqueId"). Full build: import _ from "lodash" then _.uniqueId.

Next: _.VERSION

Read the installed Lodash semantic version string.

_.VERSION 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.

10 people found this page helpful