Reference article · Programming
UUID identifiers
A UUID is an identifier used to distinguish items. Applications can use it as a reference for a record or object.
What it identifies and guarantees
UUID means universally unique identifier. The format has 128 bits, but uniqueness is not a mathematical guarantee for every generated value: collisions remain possible. Use a database uniqueness constraint when duplicates must be rejected.
Version 4 UUID
Version 4 has 122 random bits; the others identify version and variant. Browser crypto.randomUUID() produces v4 using a cryptographically secure generator. Other UUID versions use different generation methods.
Format
The result is a string of groups separated by hyphens. This illustrates its shape; do not reuse it for every record.
36b8f84d-df4e-4d49-b662-bcde71a8764f
JavaScript usage
In browsers, randomUUID() requires a secure context such as HTTPS. Check availability. Generate a new value for each item, but do not regenerate an existing record’s identifier every time it is displayed.
const article = {
id: crypto.randomUUID(),
title: "Introduction to HTML",
};
console.log(article.id);Identification and permissions
An identifier does not replace access checks. An API receiving a record UUID must still verify the user’s permission. This example is not a complete authentication or account-recovery protocol.
Practice
- Generate identifiers for three different articles.
- Check the hexadecimal group lengths: 8-4-4-4-12.
- Keep the same id when editing an article’s title.
References
Related articles
- Fundamentals: variables, conditions and loops
- Semantic HTML and page structure
- CSS: box model, Flexbox and Grid
- JavaScript: functions, parameters and scope
- JavaScript: arrays, objects and data transformations
- DOM and events: connect JavaScript to HTML
- Promises, async/await and fetch requests
- HTTP and web APIs: requests, responses and status codes
- JSON: format, parsing and data validation
- Git: changes, commits, branches and remotes
- Node.js and npm: project scripts and dependencies
- Debugging and tests: finding and checking errors
- Colour formats: HEX, RGB and HSL
- Random values in JavaScript
