so.cl

Rascals are always sociable, and the chief sign that a man has any nobility in his character is the little pleasure he takes in others company. Arthur Schopenhauer


How do you generate an UUID in JavaScript?

How do you generate an UUID in JavaScript?

Since Crypto API is standard in all modern browsers, it’s as simple as invoking a method from the crypto object:

crypto.randomUUID();

And validating it is just a matter of specifying a regular expression and calling the test() method on it.

let regex = /^[a-z,0-9,-]{36,36}$/;
let uuid = crypto.randomUUID();
/* Will return true if the regex is valid */
regex.test(uuid);