In previous article, I have explained about Uniqueidentifier (GUID/UUID) in SQL Server, now in this article, I am going to provide few possible ways to generate GUID / UUID using Javascript.

GUID (Globally Unique Identifier) or (UUID) Universally Unique Identifier is a 16 byte binary value and are identifiers designed to provide certain uniqueness guarantees.

### Generate GUID using Math.Random()

To generate GUID using Math.Random() in Javascript, you can use the below code.

``````function uuidv4() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
}
``````

Since, GUID created above is highly relies on `Math.Random()`, which can be good source of creating GUID, but it cannot considered as high-quality Random Number generator.

So, there can be chance of Collisions of GUID, but can be used to small number of generation and we will move on to next method.

A better implementation of `Math.Random()` to generate GUID would be to use Date and Time with it, so it can be unique.

So, here is the Javascript code for it

``````function generateUUIDUsingMathRandom() {
var d = new Date().getTime();//Timestamp
var d2 = (performance && performance.now && (performance.now()*1000)) || 0;//Time in microseconds since page-load or 0 if unsupported
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random() * 16;//random number between 0 and 16
if(d > 0){//Use timestamp until depleted
r = (d + r)%16 | 0;
d = Math.floor(d/16);
} else {//Use microseconds since page-load if supported
r = (d2 + r)%16 | 0;
d2 = Math.floor(d2/16);
}
return (c === 'x' ? r : (r & 0x3 | 0x8)).toString(16);
});
}
``````

Sample Output, when above code is used

``````56648260-754f-4f09-b256-a73124f12c21

1aa3bdae-974f-4455-97f6-316f75f20464
``````

Here is the complete JS fiddle

### Generate GUID using Crypto API in JS

The `Crypto` interface represents basic cryptography features available in the current context.

It allows access to a cryptographically strong random number generator and to cryptographic primitives, so here is Javascript sample code to generate GUID using it.

``````function uuidv4() {
return ([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g, c =>
(c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16)
);
}``````

### GUID using Pseudo-Random Number

Here is another example

``````function createUUID() {
var s = [];
var hexDigits = "0123456789abcdef";
for (var i = 0; i < 36; i++) {
s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
}
s[14] = "4";  // bits 12-15 of the time_hi_and_version field to 0010
s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1);  // bits 6-7 of the clock_seq_hi_and_reserved to 01
s[8] = s[13] = s[18] = s[23] = "-";

var uuid = s.join("");
return uuid;
}``````

### Fast GUID method

You can also use simple below code, which is fast to generate an ASCII-safe GUID

``````function generateGuidQuickly() {
return Math.random().toString(36).substring(2, 15) +
Math.random().toString(36).substring(2, 15);
}``````

There are many other ways, but these are best one which you can use to generate GUID using Javascript.

If you are considering any of the above solution, then I would recommend you to use, Math.Random with Datetime generator, which will make sure that no 2 GUID can be same.

You may also like to read:

How to remove element from an array in Javascript?

Creating copy to clipboard using Javascript or jQuery

Autocomplete Textbox using Javascript (Multiple Methods)

Read or Generate QR Code using Javascript

Toggle Class (Javascript based, without jQuery)