Skip to content

How to Generate a UUID / GUID in JavaScript & Node.js (Native)

Generate cryptographically secure UUID v4 / GUID in JavaScript using crypto.randomUUID(). Works natively in modern browsers and Node.js without npm packages.

Browser & Runtime Support: Chrome 92+, Edge 92+, Firefox 95+, Safari 15.4+, Node.js 16.7+, Bun, Deno.

For modern browsers and Node.js, generate a cryptographically secure UUID v4 with crypto.randomUUID(). It is faster, native, and significantly safer than custom Math.random() implementations.

Need to handle raw bytes or hashes? See our guides on converting strings to Uint8Array and Web Crypto AES encryption.

const id = crypto.randomUUID();

A GUID or UUID is a 128-bit identifier commonly used for:

  • Identifying users or sessions
  • Generating unique keys in databases
  • Preventing collisions in distributed systems

A typical UUID looks like this:

550e8400-e29b-41d4-a716-446655440000

This is known as UUID v4, which is randomly generated.


πŸ§ͺ Native JavaScript: Generate UUID Without External Libraries

There are two main methods to generate UUIDs in JavaScript without libraries:


This is the most secure and browser-friendly way (ES6+), using the built-in Web Crypto API.

function generateUUIDv4() {
  return ([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g, c =>
    (c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16)
  );
}

console.log(generateUUIDv4());

πŸ”’ Why use crypto.getRandomValues()?

  • Cryptographically secure
  • Works in all modern browsers
  • No need for external packages

βœ… Output Example:

a52c7e30-5ef9-4b73-9b82-0984c066a763

πŸ” 2. UUID-like Fallback with Math.random() (Not secure)

Only use this method if security is not a concern (e.g., local dev tools or mock data).

function generateFakeUUID() {
  let d = new Date().getTime();
  if (typeof performance !== 'undefined' && typeof performance.now === 'function'){
    d += performance.now(); // more uniqueness
  }

  return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
    const r = (d + Math.random()*16)%16 | 0;
    d = Math.floor(d/16);
    return (c === 'x' ? r : (r&0x3|0x8)).toString(16);
  });
}

console.log(generateFakeUUID());

⚠️ Warning:

This method is not safe for production use cases involving identity, authentication, or sensitive data.


πŸ“Š Performance & Browser Support

Feature Support
crypto.getRandomValues βœ… All modern browsers
Math.random fallback βœ… Universal
UUID v4 format βœ… Manually implemented

βœ… No Need for uuid NPM Package

While uuid is a great library for Node.js or build-time tools, using native JavaScript lets you:

  • Reduce bundle size
  • Avoid external dependencies
  • Stay fast and lightweight

πŸ“Ž Use Cases in Web Apps

You can use these UUIDs for:

  • Generating unique IDs for HTML elements
  • Creating session tokens or client IDs
  • Identifying objects in offline-first apps
  • Cache-busting keys or localStorage keys

🧠 Conclusion

Generating UUIDs in the browser doesn’t require any external library. With modern JavaScript, especially the Web Crypto API, you can securely and efficiently create UUIDs in a few lines of code.

For secure, production-level identifiers: βœ”οΈ Use crypto.getRandomValues()

For local dev, quick mockups: βž– You can use a Math.random() fallback


πŸ” FAQs

Q: Is UUID same as GUID? A: Technically, GUID is Microsoft’s implementation of UUID, but they are used interchangeably in most frontend contexts.

Q: Can I use these in React/Next.js/Vue? A: Absolutely. These native functions work in any frontend framework or plain JavaScript app.

Q: Is crypto.getRandomValues available in all browsers? A: Yes, it’s supported in all modern browsers including Chrome, Firefox, Safari, Edge, and mobile versions.



πŸ“₯ Copy & Paste Snippet

function uuid() {
  return ([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g, c =>
    (c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16)
  );
}

Topics & Technologies

JavaScript UUIDGUID JavaScriptgenerate UUID in browserUUID v4 JavaScriptcreate GUID JavaScriptJavaScript crypto UUIDno library UUID JavaScriptsecure UUID JavaScriptuuid without npmuuid native javascript

Continue Reading

View all β†’