how to generate a uuid in javascript

To generate a UUID in JavaScript, we can use the uuid package. It provides functions to generate UUID version 1, 3, 4, and 5.

To install the package, run the following command:

npm install uuid
17 chars
2 lines

To generate a UUID version 4, we can use the uuid.v4() function:

index.tsx
const uuid = require('uuid');

const myUUID = uuid.v4();

console.log(myUUID); // e.g. 45745c60-7b1a-11e8-9c9c-2d42b21b1a3e
124 chars
6 lines

This will generate a random UUID string that is unique and can be used as an ID.

gistlibby LogSnag