What is a UUID?
A UUID, or a Universally Unique Identifier, is a 128-bit value. It creates unique identifiers across different systems. It is basically a unique ID, which makes it easier to identify resources without overlap.
UUIDs are represented in a canonical string formatting of 36 characters. They combine hexadecimal numbers and hyphens. There are multiple versions of UUIDs, each providing different generation options such as random values, time-based, or a namespace identifier. UUID v4 is the most commonly used version, which uses random bits or random numbers for its output.
UUID Length and Collisions
The length of a UUID is fixed at 128 bits. This means there are more than 3.4×10^38 possible values, which makes repetition almost impossible. In other words, the chances of two UUIDs being identical are significantly low. Although collisions are possible, the chances are so low that it is negligible, even with billions of generated UUIDs.
Using UUID standards means that two different systems producing two different identifiers will not conflict. This reliability comes from its variant bits and strict UUID generation rules.
How to Generate UUID in Node.js
We can generate a UUID in node.js or JavaScript with a JavaScript UUID library. Or we can use the built-in crypto module to get a version 4 identifier:
| const { randomUUID } = require(‘crypto’); console.log(randomUUID()); |
Many developers also use other popular JavaScript UUID library packages, which support multiple variants, including UUID v4. These JavaScript libraries make generating UUIDs easier in JavaScript environments.
UUID Examples
Here are examples of 4 UUIDs:
- 123e4567-e89bw-12d3-a456-426614174000
- 550e8400-e29b-41d4-a716-446655440000
- 6fa459ea-ee8a-3ca4-894e-db77e160355e
- 9a49997e-1c3a-478b-9f8f-729a0b6aef6d
These examples show random UUIDs with proper formatting. You can create UUIDs for your apps by implementing the UUID standards.