JavaScript
Its easy to make GUIDs in JavaScript. Modern browsers (2021+) support the native Crypto API which generates cryptographically secure UUIDs that conform to RFC 4122 version 4.
Modern Method (Recommended - Browsers & Node.js 15+)
// Simple and secure - built into modern browsers and Node.js
let guid = crypto.randomUUID();
console.log(guid); // e.g., "36b8f84d-df4e-4d49-b662-bcde71a8764f"
Legacy/Node.js Method
For older browsers or Node.js, use the popular uuid package:
// Install via npm: npm install uuid
import { v4 as uuidv4 } from 'uuid';
let guid = uuidv4();
console.log(guid);
Manual Implementation (Legacy)
For environments without crypto support, here's a manual implementation. Note: This is not cryptographically secure and should only be used as a last resort.
function generateGUID() {
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);
});
}
let guid = generateGUID();
console.log(guid);