Create GUID / Create UUID in JavaScript Create GUID / Create UUID in JavaScript

Javascript create guid

Generate uuid

Most server-side languages and databases provide built-in support for UUIDs (Universally Unique IDentifier), also known as GUIDs (Globally Unique IDentifier). At the time of writing JavaScript does not, so let's create our own UUID generator with JavaScript array.

There are a few other examples out there that use random numbers and date parts; however, I found this solution that leverages window.crypto.getRandomValues. There are also of course of few libraries out there, most notably node-uuid (npm install uuid) for your universally unique identifier, but sometimes it's fun to create things on our own for experimentation with the crypto api to generate UUID.

Generate uuid universally unique identifier with Javascript code

Using window.crypto.getRandomValues to create a UUID/GUID with the crypto api

Let's take a look at the function that will return UUID after it generate GUID from a byte array. It uses a pseudo random number when creating guid with the following code function createuuid. This example is very similar to using the JavaScript String concat() Method

Function generateuuid or function createuuid (whichever you prefer) to generate guid or create guid or uuid in javascript with the crypto api:

function createuuid() {
let bytes = window.crypto.getRandomValues(new Uint8Array(32));
const randomBytes = () => (bytes = bytes.slice(1)) && bytes[0];
return ([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, c =>
(c ^ randomBytes() & 15 >> c / 4).toString(16)

UUID in Javascript with crypto api

Generating uuids

Let's execute it 10 times generate UUID to create guid and checkout the output to see the UUID generated with the below code for a certain uniqueness guarantees in the example guid for the uuid in javascript using:

for (var i = 0; i < 10; i++) {

Generate uuid

guid uuid in javascript

When I ran the function method I received the following sample output for the UUID generated:











Looks pretty good to me. Let's run it again to see some more UUIDs:











With this code it's like we have our own uuid package to generate uuid in javascript using a random number and not relying on a new date for the uuid in javascript.

Function createuuid

Creating guid uuid in javascript

I've previously accomplished this by executing a server-side function to generate a GUID (globally unique identifier) with the crypto api and pass it through to my JavaScript with the below code. This saves me the awkwardness of passing a variable from the server to the client when I generate guid to return uuid in javascript.

Generate guid

uuid package with crypto api and a random number

Some other ways to generate uuid would be to use var uuid or import uuid in javascript using randomuuid instanceof function to return random with the following methods for the uuids generated. This is great that it will work in both older browsers and modern browsers without using regular expression. The following code can be executed on page load and contains a small footprint for the identifiers designed with the allowed values.

Published on Jul 6, 2022

Tags: JavaScript | guid | uuid

Related Posts

Did you enjoy this article? If you did here are some more articles that I thought you will enjoy as they are very similar to the article that you just finished reading.


Learn how to code in HTML, CSS, JavaScript, Python, Ruby, PHP, Java, C#, SQL, and more.

No matter the programming language you're looking to learn, I've hopefully compiled an incredible set of tutorials for you to learn; whether you are beginner or an expert, there is something for everyone to learn. Each topic I go in-depth and provide many examples throughout. I can't wait for you to dig in and improve your skillset with any of the tutorials below.