10 examples of 'javascript generate unique id' in JavaScript

Every line of 'javascript generate unique id' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
6function createId () {
7 return (
8 Math.random().toString(16).substring(2)
9 + Math.random().toString(16).substring(2)
10 )
11}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

2function generateID() {
3 let str = '';
4 for (let i = 0; i < 16; i += 1) {
5 str += chars[Math.floor(Math.random() * chars.length)];
6 }
7 return str;
8}
6function generateId() {
7 let text = "";
8 const possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
9
10 for (let i = 0; i < 60; i++)
11 text += possible.charAt(Math.floor(Math.random() * possible.length));
12
13 return text;
14}
5function createId() {
6 return prefix + Date.now();
7}
47export function autoId(): string {
48 const chars =
49 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
50 let autoId = '';
51 for (let i = 0; i < 20; i++) {
52 autoId += chars.charAt(Math.floor(Math.random() * chars.length));
53 }
54 return autoId;
55}
22function generateNewId() {
23 var objectId = new ObjectId();
24 document.getElementById("timeStamp").value = objectId.getDate();
25 document.getElementById("machine").value = objectId.machine;
26 document.getElementById("pid").value = objectId.pid;
27 document.getElementById("increment").value = objectId.increment;
28 if (typeof (JSON) == 'object' && typeof (JSON.stringify) == 'function') {
29 var txtNode = document.createTextNode('Raw: ');
30 var element = document.createElement('pre').appendChild(document.createTextNode(JSON.stringify(objectId)));
31 var rawObject = document.getElementById("rawObject");
32 while (rawObject.childNodes.length > 0) {
33 rawObject.removeChild(rawObject.childNodes[0]);
34 }
35 rawObject.appendChild(txtNode);
36 rawObject.appendChild(element);
37 }
38}
10function makeId() {
11 return '_' + Math.random().toString(36).substr(2, 9);
12};
43export function genId() {
44 return (
45 Math.random()
46 .toString(36)
47 .substring(2, 15) +
48 Math.random()
49 .toString(36)
50 .substring(2, 15)
51 )
52}
382function makeID () {
383 return 'containerid-' + Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1)
384}
83function generateID(){
84 // generate random 5 character id for the session
85 var d = new Date().getTime();
86 if(window.performance && typeof window.performance.now === "function"){
87 d += performance.now();
88 }
89 var uuid = 'xxxxx'.replace(/[xy]/g, function(c) {
90 var r = (d + Math.random()*16)%16 | 0;
91 d = Math.floor(d/16);
92 return (c=='x' ? r : (r&0x3|0x8)).toString(16);
93 });
94 return uuid;
95}

Related snippets