To generate a unique ID with JavaScript, we can use the uuid
package.
To install it, we run:
npm i uuid
Then we can use the package to generate some unique IDs by writing:
const uuid = require('uuid');
const {
v1: uuidv1,
v4: uuidv4,
} = require('uuid');
console.log(uuidv1())
console.log(uuidv4())
We use the uuidv1
function to generate v1 UUIDs and the uuidv4
function to generate V4 UUIDs.
Then we get something like:
62d097d0-df74-11eb-ba01-719ac742ff00
06bbeb19-754d-4092-97a0-7ab61d394d05
as a result from the console log.
Conclusion
To generate a unique ID with JavaScript, we can use the uuid
package.