/* Codeboosh */

How to Create a GUID in Vanilla JavaScript

There are lots of times when you need a GUID or UUID when writing JavaScript, for example when you need to create a unique ID for an input, and luckily there is now a super simple way to generate one.

The Web Crypto API provides a lot of useful functions for building systems using cryptography, however the one I use the most is the crypto.randomUUID() method.

This simple one liner creates a randomly generated 36 character version 4 UUID.

const uuid = crypto.randomUUID();
console.log(uuid); // "5abb7237-d597-46ad-904f-9f6314c39b78"

What is the browser support for randomUUID?

The browser support for randomUUID is really good with almost all modern browsers supporting it. The main browser that doesn’t support it is IE11 and if you are lucky enough not to support IE anymore, then you are good to go. It’s even supported on the server-side with Node.js 19+ and Deno 1.1.1+.

👍 Thanks for reading. Have a great rest of your day.