10 examples of 'javascript uuid generator' in JavaScript

Every line of 'javascript uuid generator' 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
19function generatorUUID() {
20 var d = new Date().getTime();
21 var uuid = "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function (c) {
22 var r = (d + Math.random() * 16) % 16 | 0;
23 d = Math.floor(d / 16);
24 return (c === "x" ? r : (r & 0x3) | 0x8).toString(16);
25 });
26
27 return uuid;
28}
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

61function generateUuid() {
62 return Math.random().toString() +
63 Math.random().toString() +
64 Math.random().toString();
65}
9export function uuid(length: number = 32): string {
10 let s = new Array<string>(length);
11 let hexDigits = '0123456789abcdef';
12 let nChars = hexDigits.length;
13 for (let i = 0; i < length; i++) {
14 s[i] = hexDigits.charAt(Math.floor(Math.random() * nChars));
15 }
16 return s.join('');
17}
13export function uuid(a = undefined)
14{
15 // https://gist.github.com/jed/982883
16 return a?(a^Math.random()*16>>a/4).toString(16):([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g,uuid);
17}
8function generateUuid() {
9 let uuid = "",
10 i,
11 random;
12 for (let i = 0; i < 32; i++) {
13 random = (Math.random() * 16) | 0;
14
15 if (i == 8 || i == 12 || i == 16 || i == 20) {
16 uuid += "-";
17 }
18 uuid += (i == 12 ? 4 : i == 16 ? (random & 3) | 8 : random).toString(16);
19 }
20 return uuid;
21}
5export default function uuid () {
6
7 let uuid = ''
8 let i
9 let random
10
11 for (i = 0; i < 32; i++) {
12 random = Math.random() * 16 | 0
13 if (i === 8 || i === 12 || i === 16 || i === 20) {
14 uuid += '-'
15 }
16 uuid += (i === 12 ? 4 : (i === 16 ? (random & 3 | 8) : random)).toString(16)
17 }
18
19 return uuid
20
21}
78function generateUUID() {
79 var d = new Date().getTime();
80 var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
81 var r = (d + Math.random()*16)%16 | 0;
82 d = Math.floor(d/16);
83 return (c=='x' ? r : (r&0x3|0x8)).toString(16);
84 });
85 return uuid;
86};
35export function UUID (): string {
36 function s4 () {
37 return Math.floor((1 + Math.random()) * 0x10000)
38 .toString(16)
39 .substring(1)
40 }
41 return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
42 s4() + '-' + s4() + s4() + s4()
43}
6function generateUUID() { // Public Domain/MIT
7 var d = new Date().getTime();
8 if (typeof performance !== 'undefined' && typeof performance.now === 'function') {
9 d += performance.now(); //use high-precision timer if available
10 }
11 return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
12 var r = (d + Math.random() * 16) % 16 | 0;
13 d = Math.floor(d / 16);
14 return (c === 'x' ? r : (r & 0x3 | 0x8)).toString(16);
15 });
16}
5function createUUID () {
6 return String(Date.now() + Math.random());
7}

Related snippets