Create a GUID / UUID in Javascript

What is the GUID / UUID ?

A GUID is Microsoft’s implementation of a unique identifier(UUID).


Accroding to Wikipedia:

“Universally Unique Identifier (UUID) is an identifier standard used in software construction, standardized by the Open Software Foundation (OSF) as part of the Distributed Computing Environment (DCE).”


A UUID is a 16-octet (128-bit) number.

A UUID is represented by 32 lowercase hexadecimal digits and it displayed in 5 groups separated by hyphens(-).

This represent in form of 8-4-4-4-12 for a total of 36 characters (32 alphanumeric characters and four hyphens).


For example:



There are few methods to generate GUID / UUID in javascript.

Method 1:

  function getGUID() {
     return 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'.replace(/[x]/g, function(c) {
         var num = Math.random() *16 | 0, v = c === 'x' ? num : (num&0x3|0x8);
         return v.toString(16);

Output: “258b8c84-0e87-707e-cf8d-bdc8eb32d9c5″


Method 2:

 function getGUID() {
   function fd() {
     return Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1);
     return fd() + fd() + '-' + fd() + '-' + fd() + '-' + fd() + '-' + fd() + fd() + fd();
  Output: "40bcc30d-94f9-b7bb-a464-391e243433a1"


These functions generates many UUID. But true GUID can not be generated with JavaScript.





One thought on “Create a GUID / UUID in Javascript

  1. You are wrong. It can be generated!
    I like the idea but what you are missing is Pythia. Look at my near-True Random Number Generator.
    I am sure that after you put Pythia in your code the GUID will be a 100%

    Great job so far.

Comments are closed.