Skip to content

Latest commit

 

History

History
24 lines (19 loc) · 798 Bytes

generate-a-v4-uuid-in-the-browser.md

File metadata and controls

24 lines (19 loc) · 798 Bytes

Generate A V4 UUID In The Browser

The Web Crypto API provides a randomUUID function for generating v4 UUID values which are cryptographically random.

It works like this:

> crypto.randomUUID()
'f2050c5e-af52-4ca2-b4d6-23758b3396c9'
> crypto.randomUUID()
'079d5186-84d4-41d6-a660-edafb6a74c86'

No two UUIDs will be the same. This is a great way to generate IDs that are guaranteed to be unique. Or if you need a value that is practically impossible to guess.

I was surprised to see that as of writing this, this function has great browser support. Modern versions of all browsers except Internet Explorer have implemented randomUUID.