Universally Unique Identifiers (UUIDs) are used all over the place in software development for everything from identifying object elements to DOM elements on a web page. They are unique, 128-bit identifiers consisting of 36 characters in the format - for example, . 8-4-4-4-12 f81e7af3-fcf4-4cdd-b3a3-14a8087aa191 UUIDs typically do not rely on a registry or database to ensure uniqueness. The chances of a UUID being a duplicate is not zero, but it is so close that most ignore the risk. In Javascript, there are many ways to create UUIDs. With that in mind, let's look at how you can . create UUIDs with Javascript Option 1: use the crypto.randomUUID() method is a relatively new and reliable way of making UUIDs with native Javascript. It's supported in all modern, evergreen browsers and can generate a UUID with one line of code. The method is available on the global object on browsers - also known as . It is also available on Node.JS. crypto.randomUUID crypto this window You can now generate UUIDs like so in the browser: console.log(this.crypto.randomUUID()); // f81e7af3-fcf4-4cdd-b3a3-14a8087aa191 Or, in Node.JS from version like this: 19.0 import crypto from 'crypto' console.log(crypto.randomUUID()); // f81e7af3-fcf4-4cdd-b3a3-14a8087aa191 Option 2: use the UUID package There is a very popular package on NPM called which does all the grunt work for you and generates reliable UUIDs. It can be used in a Node.JS project by first installing it, and then running it in your code. uuid First, use to install: npm npm i uuid Then you just have to import and use it in your code like this: import { v4 as uuid } from 'uuid'; let myUUID = uuid(); // Returns a random UUID xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx The package is a reliable method of creating UUIDs in Node.JS. uuid Option 3: use the crypto.getRandomValues() method It is possible to generate your own function using . This is useful in situations where you're dealing with Node.JS versions before , or have to support old browsers. is supported as far back as Chrome 11. uuid crypto.getRandomValues() 19.0 crypto.getRandomValues() function uuid() { return ('10000000-1000-4000-8000-100000000000').replace(/[018]/g, c => ( c ^ (crypto.getRandomValues(new Uint8Array(1))[0] & (15 >> (c / 4)))).toString(16) ); } This function is based - and generates reliable UUIDs if you need your own function to do so. off the work here Option 4: Math.random() (not recommended) The last resort, and the dirtiest solution, is to use , however, this is not generally a good idea, since does not do a sufficiently good job of generating UUIDs to enure uniqueness. You should also not need this since we now have the method widely available. Math.random() Math.random() crypto.randomUUID() If you want a real UUID, it's best to avoid this solution, but it can provide a quick fix on a project where you need a UUID fast for testing. This could be acceptable as a fallback when you don't have support or the package available: randomUUID() uuid function uuid() { 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); }); } Conclusion That about covers all the different ways to make UUIDs in Javascript. Hope you've enjoyed this guide - . check out more about Javascript here Also published here.