DEV Community

Discussion on: How to Generate Unique ID in JavaScript

Collapse
 
coderkearns profile image
coderkearns

You could decrease the chances by using multiple of the author's ways:

Maybe

function uniqueID() {
return Math.floor(Math.random() * Date.now())
}

You could even string multiple randoms together:

Math.floor(Math.random() * Math.floor(Math.random() * Date.now()))

Collapse
 
wolffe profile image
Ciprian Popescu

Depending on the application, this is the right way to do it. In my case, I don't have a loop, I have several elements being displayed on subsequent clicks, and there's several seconds between each click. So there's zero chances of an identical ID.

Collapse
 
abbatyya profile image
abbatyya

thank you, this work also. to also include string i use Math.floor(Math.random() * Date.now()).toString(16)