UUIDs (Universally Unique Identifiers) are used to uniquely identify information in a distributed system. Different UUID versions offer various methods for generating these identifiers based on time, randomness, or a name-based hashing scheme.
A v1 UUID is generated using the current timestamp (time-based) and the machine’s network address (usually the MAC address). This ensures that the generated UUID is unique across both time and space.
A v4 UUID is generated using random values. It is the most widely used UUID version because it offers a very high likelihood of uniqueness without requiring a timestamp or machine address.
A v5 UUID is generated by hashing a namespace and a name. It is used to generate unique identifiers based on specific names, which ensures that the same name in the same namespace will always generate the same UUID.
To generate UUIDs in JavaScript, you can use the uuid
library to generate UUIDs. Below are examples of how to generate UUID v1, v4, and v5 using JavaScript.
You can include the uuid
library in your project via CDN for web projects, or install it via npm for Node.js:
<script src="https://cdnjs.cloudflare.com/ajax/libs/uuid/8.3.2/uuid.min.js"></script>
npm install uuid
// Importing uuid (if using Node.js, or use the CDN version for browsers) const { v1, v4, v5 } = uuid; // Generate a Time-based (v1) UUID const v1UUID = v1(); console.log("v1 UUID:", v1UUID); // Generate a Random-based (v4) UUID const v4UUID = v4(); console.log("v4 UUID:", v4UUID); // Generate a Name-based (v5) UUID (using DNS namespace) const v5UUID = v5("example.com", v5.DNS); console.log("v5 UUID:", v5UUID);