Generate UUID in JavaScript Without Libraries!

You'll often find it helpful to be able to generate UUIDs in JavaScript for use in your apps.

Until recently, I achieved this easily by installing something like uuid on npm.

But we can do this with the built-in Crypto web API.

How?

Simply call crypto.randomUUID() in your code, and it will generate you a unique UUID.

Example:

// logs a new UUID each time you run the script
console.log(crypto.randomUUID())
// Example output: '14ae278c-e736-49f5-aa3f-f921cc77bc64' 

All your modern browsers and Node v19+ support it.

Try it out and get rid of another module from your bundle. 🙈

Read more about the Crypto API here.

Happy coding! ✨


Follow me on Twitter or connect on LinkedIn.

🚨 Want to make friends and learn from peers? You can join our free web developer community here. 🎉

JSJavaScript
Avatar for Niall Maher

Written by Niall Maher

Founder of Codú - The web developer community! I've worked in nearly every corner of technology businesses; Lead Developer, Software Architect, Product Manager, CTO and now happily a Founder.

Loading

Fetching comments

Hey! 👋

Got something to say?

or to leave a comment.