Skip to content
Related Articles

Related Articles

Save Article
Improve Article
Save Article
Like Article

Lodash _.uniqueId() Method

  • Last Updated : 09 Sep, 2020

The Lodash _.uniqueId() method is used to create an unique id for some 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 always even if the project restarts. 

Syntax:

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!

_.uniqueId(prefix)

Parameters: This method accepts a single parameter as mentioned above and described below:

  • prefix: The value to prefix the ID with.

Return Value: This method returns the string unique ID.



Example 1:

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:

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
My Personal Notes arrow_drop_up
Recommended Articles
Page :