Developer's Center
JavaScript

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

JavaScript, the widely used programming language, has become the backbone of web development. In this guide, we will explore different methods to generate Universally Unique Identifiers (UUIDs) in JavaScript.

Generating UUIDs with the uuid Library

While JavaScript doesn't have built-in support for UUID generation, you can utilize third-party libraries. One popular library is "uuid," which supports generating version 1, 3, 4, and 5 UUIDs.

To get started with the uuid library, install it by running the following command if your project has a package.json file:

% npm install uuid

Alternatively, you can install it globally on your computer using this command:

% npm install -g uuid

Once the library is installed, you can generate a version 4 UUID using the following code:

import {v4 as uuidv4} from 'uuid';

let myuuid = uuidv4();

console.log('The UUID is: ' + myuuid);

In the above code, we import the v4 function from the uuid library, which generates a version 4 UUID. The uuidv4() function is then called to generate the UUID, which is stored in the myuuid variable. Finally, we print the generated UUID to the console.

Generating UUIDs in a Browser

If your JavaScript code runs in a secure browser context, you can use the browser's crypto object to generate a cryptographically secure version 4 UUID. Here's how:

let myuuid = crypto.randomUUID();
 
console.log('The UUID is: ' + myuuid);

In the above code, the randomUUID() function provided by the crypto object generates a version 4 UUID. The generated UUID is then stored in the myuuid variable, and it is printed to the console.

Please note that using the crypto object is recommended only in secure contexts, such as HTTPS.

Other Options for Generating UUIDs in Javascript

Apart from the uuid library, there are other options available for generating UUIDs in JavaScript. One such option is the uuidv7 package, which supports version 7 UUIDs. To use it, follow these steps:

  1. Install the uuidv7 package using the command:
npm install uuidv7
  1. Import the uuidv7 function and generate a version 7 UUID:
import { uuidv7 } from 'uuidv7';
 
let myuuid = uuidv7();
 
console.log('The UUID is: ' + myuuid);

The uuidv7() function generates a version 7 UUID, which is stored in the myuuid variable. Finally, the generated UUID is printed to the console.

Conclusion

Generating UUIDs in JavaScript is made easy by utilizing libraries like uuid or browser APIs like crypto.randomUUID(). By following the examples and explanations provided in this guide, you can generate unique identifiers for your JavaScript applications.

Remember to choose the appropriate method based on your requirements and the context in which your code runs. Happy coding with JavaScript and UUID generation!