Friday, September 19, 2025
HomeLanguagesJavascriptLodash _.uniqueId() Method

Lodash _.uniqueId() Method

Lodash _.uniqueId() method is used to create a unique ID for an element each time. This method will work for the purpose of assigning a unique ID for most use cases, but not with complex projects that require a unique ID even if the project restarts. 

Syntax:

_.uniqueId([prefix = ''])

Parameters:

  • prefix is the value to prefix the ID with.

Return Value:

This method returns the string’s unique ID.

Example 1: In this example, we are printing the unique id in the console by using the _.uniqueId() method.

Javascript




// Requiring the lodash library 
const _ = require("lodash");
 
// Use of _.uniqueId() 
// Method without the prefix
let gfg = _.uniqueId();
 
// Printing the output 
console.log(gfg);


Output:

1

Example 2: In this example, we are printing the unique id in the console by using the _.uniqueId() method having ‘gfg’ as a prefix.

Javascript




// Requiring the lodash library 
const _ = require("lodash");
 
// Use of _.uniqueId() 
// Method with prefix
let ans = _.uniqueId("gfg_");
 
// Printing the output 
console.log(ans);
 
let ans1 = _.uniqueId("gfg_");
 
// Printing the output 
console.log(ans1);


Output:

gfg_1
gfg_2
Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

Most Popular

Dominic
32301 POSTS0 COMMENTS
Milvus
84 POSTS0 COMMENTS
Nango Kala
6666 POSTS0 COMMENTS
Nicole Veronica
11840 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11898 POSTS0 COMMENTS
Shaida Kate Naidoo
6781 POSTS0 COMMENTS
Ted Musemwa
7056 POSTS0 COMMENTS
Thapelo Manthata
6739 POSTS0 COMMENTS
Umr Jansen
6744 POSTS0 COMMENTS