crypto.randomUUID
Returns a newly generated, version 4 UUID string in the form xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx
Syntax
crypto.randomUUID()Return Value
A string containing a randomly generated 36-character UUID v4
Examples
const id = crypto.randomUUID()
console.log(id) // e.g. '550e8400-e29b-41d4-a716-446655440000'const items = Array.from({ length: 5 }, () => ({
id: crypto.randomUUID(),
createdAt: Date.now()
}))
console.log(items)function createRecord(name: string) {
return {
id: crypto.randomUUID(),
name,
timestamp: new Date().toISOString()
}
}Understanding crypto.randomUUID
The crypto.randomUUID method in JavaScript returns a newly generated, version 4 UUID string in the form xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx. It belongs to the Crypto object and is one of the most widely used methods for working with crypto values in modern JavaScript and TypeScript applications.
The method signature is crypto.randomUUID(). When called, it returns a string containing a randomly generated 36-character uuid v4. Understanding when and how to use randomUUID() helps you write more expressive, readable code.
Common use cases for crypto.randomUUID include data transformation, input validation, API response processing, and building reusable utility functions. It works well alongside related methods like crypto-getrandomvalues, math-random, enabling you to chain operations together for complex data manipulation pipelines.
Supported in Chrome 92+, Firefox 95+, Safari 15.4+, Edge 92+, and Node.js 19+. Requires a secure context (HTTPS).
Browser Compatibility
Supported in Chrome 92+, Firefox 95+, Safari 15.4+, Edge 92+, and Node.js 19+. Requires a secure context (HTTPS).
Related Methods
More Crypto Methods
Other methods in the Crypto object
Related Tools
More Crypto Methods
Explore JavaScript Methods
Browse our complete reference of 410 JavaScript methods with syntax, examples, and explanations.