How to create a UUID in JavaScript

Creating a globally unique identifier has always been a necessity in all programming languages and for some reason, JavaScript never had a way of doing it in the default spec. But that is changing now with the crypto API. We can now create a UUID in JavaScript.

What is UUID/GUID?

A universally unique identifier (UUID) is a 128-bit label used for information in computer systems. The term globally unique identifier (GUID) is also used instead of UUID.

- Advertisement -

For example: “4ab4e2a1-0efb-4e5b-8f73-e503f5b8e89f”

Create a UUID in JavaScript

Traditionally, we needed to use either Math.Random(), or the Date object and then convert it into a UUID format. But now all we need is:

crypto.randomUUID() // "819df8d4-587b-4200-90b3-d30f8ed01457"

It is important to note that it still might not generate a unique value though the probability of that happening is fairly low.

Recent Articles

How to check if a string contains emojis in JavaScript?

If you have user-generated content in your web application, chances are you have to deal with strings containing emojis. Since emojis are...

Writing conditionals in CSS: when/else

CSS already has had conditionals in the form of @media queries or @support queries to selectively apply styling to the document. But...

Retrieving content value of ::after or ::before in JavaScript

For the following element:#element::after { content: 'Custom value' }If we needed a way of...

How to permanently remove a file from Git history

We all make mistakes sometimes. Pushing files that contain some secrets or sensitive information to a Git repository is fairly common. And...

Select all text on click using CSS

When a user has to select some text on a website, they have to drag and hold their mouse, or use double-click...

Related Stories

Leave A Reply

Please enter your comment!
Please enter your name here

Hi there! Want some more knowledge?

Think that the knowledge shared is helpful? You might want to give our mailing list a try. We'll send you 2-4 emails a month, right when new posts come out.

Hi there! Want some more knowledge?

Think that the knowledge shared is helpful? You might want to give our mailing list a try. We'll send you 2-4 emails a month, right when new posts come out.