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.


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


// logs a new UUID each time you run the script
// 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. 🎉

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.


Fetching comments

Hey! 👋

Got something to say?

or to leave a comment.