How to Generate UUID in JavaScript (v4 Example)

Generating a UUID in JavaScript is simple and widely used in modern web development. UUIDs help create unique identifiers for databases, APIs, and applications.

What is a UUID?

A UUID (Universally Unique Identifier) is a 128-bit value used to uniquely identify data. The most common version is UUID v4, which is randomly generated.

Generate UUID in JavaScript (Modern Method)

The easiest way to generate a UUID in JavaScript is using the built-in crypto API:

const uuid = crypto.randomUUID();
console.log(uuid);

This method is supported in modern browsers and Node.js.

Generate UUID manually (fallback)

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

console.log(generateUUID());

Why use UUID in JavaScript?

  • Create unique IDs for users or sessions
  • Use in databases and APIs
  • Avoid collisions in distributed systems
  • Improve data tracking

Generate UUID online

If you prefer not to use code, you can generate UUIDs instantly using our tool:

👉 Use UUID Generator

Validate UUID format

Need to check if a UUID is valid? Use our validator:

👉 Use UUID Validator

Frequently Asked Questions

Is crypto.randomUUID() secure?

Yes, it uses a cryptographically secure random number generator.

Can I use UUID in all browsers?

Most modern browsers support it, but older browsers may require a fallback.

What UUID version should I use?

UUID v4 is recommended for most applications.

Conclusion

Generating UUIDs in JavaScript is easy using modern APIs. UUID v4 is the best choice for most use cases.

Use our free tools to generate and validate UUIDs instantly.

UUID Format Explained

What is UUID?

UUID vs GUID