Skip to content

Instantly share code, notes, and snippets.

@masautt
Created September 3, 2019 21:48
Show Gist options
  • Save masautt/610242ad7a4f08972a6c05994580566a to your computer and use it in GitHub Desktop.
Save masautt/610242ad7a4f08972a6c05994580566a to your computer and use it in GitHub Desktop.
How can you generate a GUID / UUID in JavaScript?
function genUUID() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
}
console.log(genUUID()) //--> 26a63d24-aef9-4047-9284-9fdb366bae95
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment