While building web applications, there are times when you have to assign a unique identifier to an object. This is where you can use a GUID (Globally Unique Identifier) and a UUID (Universally Unique Identifier). Both GUID and UUID are used interchangeably, and they play the same role, which is to generate a unique value that will not be duplicated.
In this blog, you will learn everything you need to know about creating a GUID/UUID in JavaScript. You will also learn about different methods to generate these identifiers without using any libraries. So let’s get started!
Table of Contents:
What is a GUID/UUID in JavaScript?
A UUID (Universally Unique Identifier) or GUID (Globally Unique Identifier) is a special code that is made up of 128 bits that helps you to identify something uniquely. You can take it as a digital name tag that cannot be repeated. It is usually displayed as a long string of numbers and letters that are separated by hyphens. For example, 3f2504e0-4f89-11d3-9a0c-0305e82c3301. Also, this format makes it more readable and usable on multiple systems. The main purpose of GUID or UUID is to make sure that every identifier is unique, even if they are created at the same time or on different devices.
Features of GUID/UUID
The features of GUID/UUID are given below:
- Universally Unique: The UUIDs/GUIDs are unique, even though they are generated on different computers.
- 128-bit length: They are made up of 128 bits, which provides you with many possible unique combinations.
- Standard format: They are shown as a string consisting of 36 characters with numbers, letters, and hyphens. For example: 550e8400-e29b-41d4-a716-446655440000.
- Random or Time-Based: You can create UUIDs or GUIDs using random numbers, timestamps, or with a mix of both to make sure they are unique.
Beginner-Friendly Courses That Guide You Step-By-Step Through Real Projects
Become a Web Developer – Enroll Now!
Types of UUIDs
The common types of UUIDs are mentioned below for your reference:
1. UUIDv1: This form of UUID is generated with the help of the present time and the MAC address of your device. Due to this, UUIDv1 is unique, but it can sometimes reveal information about when and where it was created.
2. UUIDv4: This is the most popular version of UUID and is created using random numbers. It does not depend on time or the hardware information. This makes it simple, fast, and secure for most cases.
3. UUIDv5: This version of UUID is generated using a fixed “name” and a “namespace”. It applies a hashing method (SHA-1) to make sure that the result is always the same as the inputs.
In JavaScript, the UUIDv4 is the most used because it creates a random and unique value every time, which is perfect for user IDs, file names, etc.
How to Create a GUID/UUID in JavaScript?
You can create a GUID or UUID in JavaScript using various methods. The most common methods are explained below:
Method 1: Using a Custom JavaScript Function
If you don’t want to install any libraries, you can use this function to generate a UUID that looks the same as version 4.
Code:
Output:
Explanation:
The above JavaScript code is used to replace each “x” and “y” pattern with random numbers. This helps you to create a string that looks like a unique ID (UUID).
Method 2: Using the crypto API (Browser Method)
Many modern browsers nowadays provide you with a built-in crypto module that gives better randomness than Math.random().
Code:
Output:
Explanation:
The above JavaScript code is used to create a UUID. This is done by replacing the characters in a pattern with secure random numbers by using the crypto tool of the browser.
Method 3: Using the UUID Library (For Node.js Applications)
If you are working with Node.js, you have to install an external package that is called uuid. The steps are given below:
Step 1: Install the uuid package
You have to run the following command in your terminal
npm install uuid
Step 2: Import the UUID function
Step 3: Generate a UUID
Output:
The approach will give you a validly formatted UUIDv4, which is applied in production.
Common Use Cases of GUID/UUID
Some common use cases of GUID/UUID are given below:
1. It gives files random names so that they don’t clash with each other.
2. Helps you to establish an alternative connection in case of such tasks as password reset or email verification.
3. Helps you to generate a unique ID for each order in an online shopping application.
4. It also helps you to store unique IDs in databases like MongoDB, so that everything stays organized and doesn’t get mixed up.
Get 100% Hike!
Master Most in Demand Skills Now!
Conclusion
In this blog, you have learnt about how to create a GUID or UUID in JavaScript by using various techniques. You can use a custom function, browser’s crypto tool, or the UUID library to create unique IDs that don’t repeat. These IDs are helpful when you need to name things in a way that avoids duplicates, like users, files, or entries in a database. You can choose any method that best fits your project. To learn more about JavaScript, go through our blog and enroll in our Web Development Course.
How to create a GUID/UUID in JavaScript – FAQs
Q1. Can two UUIDs ever be the same?
It is very rare, the UUID is intended to be unique, even when created in millions.
Q2. Do UUIDs slow down my app?
Not really, creating a UUID will be quick, and it will not have any significant impact on performance.
Q3. Can I shorten a UUID to make it look cleaner?
You can do that, but it can reduce the uniqueness, and the chances of duplicates can increase.
Q4. Is a UUID safe to use in a URL?
Yes, UUIDs are URL-safe and can be safely used in addresses of web addresses.
Q5. Can I generate UUIDs offline without internet?
Yes, all the methods shown work offline since they use local code or system features.