Generate a UUID in JavaScript
JavaScript is the most popular programming language in the world!
Since it's creation in 1995 for use in the Netscape browser, JavaScript's popularity has exploded to become the predominant language of the Web. Today, JavaScript powers everything from interactive web pages to dynamic web and mobile apps to backend web services.
How to Generate a UUID in JavaScript
Although the JavaScript language itself does not have built-in support for generating a UUID or GUID, there are plenty of quality 3rd party, open-source libraries that you can use.
The JavaScript library we recommend for generating UUIDs is called (unsurprisingly),
uuid
.
It can generate version 1, 3, 4 and 5 UUIDs.
Installing the uuid
Package
To get started with the library, you'll need to install it. If you're working in a project with a
package.json
file, run this command to add it to the dependencies list:
% npm install uuid
Or if you want to install it globally on your computer, use this command:
% npm install -g uuid
Generating UUIDs
With the uuid
library installed, you can now use it in your JavaScript code.
Here's how you can generate a version 4 UUID in JavaScript with the uuid
library:
import {v4 as uuidv4} from 'uuid';let myuuid = uuidv4();console.log('Your UUID is: ' + myuuid);
Explanation
- Line #1 imports the version 4 UUID function. There are also functions available for generating version 1, 3 and 5 UUIDs. Note that this is the ES6 module import syntax.
- Line #3 generates the UUID and saves it in the variable,
myuuid
. - The output from line #5 will be something like:
Your UUID is: c32d8b45-92fe-44f6-8b61-42c2107dfe87
The uuid
library has a number of other functions, such as for converting from a string representation of
a UUID into a byte array and back. You can read more about the
JavaScript uuid
library on it's GitHub page.
How to Generate a UUID in a Browser
If your Javascript code will execute within a secure browser context, such as on a page served via HTTPS, you can
also use the browser's global crypto
property to generate a cryptographically secure, version 4 UUID.
The crypto
object provides access to the
Web Crypto API and has functions defined
in the Crypto interface. For our discussion, we
can use the global crypto
object to generate version 4 UUIDs using the randomUUID()
function:
let myuuid = crypto.randomUUID();console.log('Your UUID is: ' + myuuid);
Explanation
- Line #1 generates the UUID and saves it in the variable,
myuuid
. - The output from line #3 will be something like:
Your UUID is: c32d8b45-92fe-44f6-8b61-42c2107dfe87
Although the crypto
object is available in both secure and insecure contexts, it is
not recommended for
use in insecure contexts.
The crypto
property be accessed globally in a browser context or web worker, e.g. crypto
or
window.crypto
, or self.crypto
depending on the context.
Other Options for Generating UUIDs in Javascript
To generate version 7 UUIDs, you can use the uuidv7 package.
// Install the uuidv7 package.// npm install uuidv7import { uuidv7 } from 'uuidv7';let myuuid = uuidv7();console.log('Your UUID is: ' + myuuid);
Explanation
- Line #2 shows the command needed to install the
uuidv7
package:npm install uuid7
- Line #4 imports the version 7 UUID function. There are also functions available for generating version 4 UUIDs as well as functions for obtaining the raw bytes.
- Line #6 generates the version 7 UUID and saves it in the variable,
myuuid
. - The output from line #8 will be something like:
Your UUID is: 018b2f0a-45a7-778b-88b7-da6933b704a3
How can we improve this page? Let us know!