How to generate UUID in JavaScript

Generating unique identifiers is essential for JavaScript applications that need to create unique keys, track items, or assign temporary IDs. As the creator of CoreUI with over 25 years of JavaScript development experience, I’ve implemented UUID generation in countless production systems. The most reliable solution is to use the native crypto.randomUUID() method available in modern browsers and Node.js. This approach generates cryptographically strong UUIDs without external dependencies.

Use crypto.randomUUID() to generate UUIDs in JavaScript.

const uuid = crypto.randomUUID()
console.log(uuid)
// Output: '9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d'

The crypto.randomUUID() method generates a version 4 UUID (random UUID) that is cryptographically strong and suitable for use as unique identifiers. This method is available in modern browsers and Node.js 14.17.0+. The generated UUID follows the standard format with 36 characters including hyphens. Each call produces a statistically unique identifier with negligible collision probability.

Best Practice Note

This is the same UUID generation approach we use in CoreUI components for generating unique element IDs and tracking keys. For older environments that don’t support crypto.randomUUID(), use the uuid library from npm with import { v4 as uuidv4 } from 'uuid' which provides the same functionality with broader compatibility.


Speed up your responsive apps and websites with fully-featured, ready-to-use open-source admin panel templates—free to use and built for efficiency.


About the Author