Why do you need GUID/UUID and why it's easier than it seems?
\n\nHave you ever wondered how websites and applications distinguish one database record from another? Or how your browser remembers that it was you who put items in the cart, not your neighbor? The secret often lies in a small but mighty string — GUID (Globally Unique Identifier) or its more precise sibling UUID (Universally Unique Identifier).
\n\nOn Stack Overflow, the question "How do I create a GUID / UUID?" gathers thousands of views. This is no coincidence. Every developer sooner or later faces the task: "I need a unique ID, but I don't want to pull in an entire library." Today we will break down this topic in JavaScript from start to finish. You will learn not only how to generate a UUID but also understand which method to choose for your task.
\n\nReady? Let's go!
\n\nWhat is UUID really? (and why it's not magic)
\n\nBefore writing code, let's understand the theory. UUID is a 128-bit number that is typically written in hexadecimal format with hyphens. It looks like this:
\n\n123e4567-e89b-12d3-a456-426614174000\n\nThe RFC 4122 standard describes several versions of UUID. The most popular ones:
\n- \n
- Version 4: Random UUID. Almost everything you find on the internet is this one. \n
- Version 1: Based on time and MAC address. Outdated, but sometimes encountered. \n
- Version 5: Generated from a name (e.g., URL) using SHA-1 hash. \n
The main property of UUID is uniqueness without a central server. The probability of collision (two IDs matching) is so small that it can be ignored. They say that if you generate a billion UUIDs per second, it would take about 100 years to create a duplicate. Not a bad guarantee, right?
\n\nThe most correct way: crypto.randomUUID()
\n\nIf you are writing modern JavaScript (ES2020+), you have a built-in solution at your fingertips — crypto.randomUUID(). This method is available in browsers and Node.js (starting from version 14.17.0).
\n\nHere's how it works:
\n\n// Browser\nconst myUUID = crypto.randomUUID();\nconsole.log(myUUID); // Output: "f47ac10b-58cc-4372-a567-0e02b2c3d479"\n\n// Node.js (modern versions)\nconst { randomUUID } = require('crypto');\nconst myUUID = randomUUID();\nconsole.log(myUUID);\n\nWhy is this the best choice?
\n- \n
- Security: Uses a cryptographically strong random number generator. No predictable patterns. \n
- Standard: Strictly conforms to RFC 4122 version 4. \n
- Performance: Written in C++ under the hood, works lightning fast. \n
- Zero dependencies: No need to install third-party libraries. \n
Important: In older browsers (Internet Explorer, Safari before 15.4) this method may be unavailable. If you support such "dinosaurs", read on — we have fallback options.
\n\nClassic hack: Math.random() + RFC 4122 template
\n\nBefore the advent of crypto.randomUUID(), developers reinvented the wheel. The most popular option is to use Math.random() to substitute digits into the UUID template. This code has been circulating on the internet for over 10 years:
\n\nfunction generateUUID() {\n return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {\n const r = Math.random() * 16 | 0;\n const v = c === 'x' ? r : (r & 0x3 | 0x8);\n return v.toString(16);\n });\n}\n\nconsole.log(generateUUID()); // Output: "9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d"\n\nHow does it work?
\n- \n
- The template
xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxxdefines the format. \n - The 'x' character