Different ways to generate GUID in javascript
- Admin
- Dec 31, 2023
- Javascript Typescript
In this blog post, We are going to learn how to generate Unique Identifiers -UUID
, GUID
, UDID
in javascript/typescript/nodejs.
Unique Identifier Generator in Javascript
In every programming language, We have a unique identifier to identify the resources or values, or references.
Various names identify the unique references. Generally, the following are frequently used in javascript applications.
- Universally Unique Identifier - UUID
- Globally Unique Identifier - GUID
- Unique Device Identifier - UDID
Javascript Universally Unique Identifier (UUID)
UUID
is a universally unique identifier. It contains 16 bytes values that can be used to identify unique values. The value represents hexadecimal
values. It uses as a primary key in the database tables to uniquely identify the rows.
16 bytes values are uniquely defined by RFC 4122🔗
Globally Unique Identifier - GUID
GUID
is the Microsoft version of UUID. You can use it alternatively both and functionality and usage are the same. Generated values also equally save format as UUID
.
Unique Device Identifier - UDID
This is mostly used in mobiles to uniquely identify the devices. It contains 40 hexadecimal characters in size. It can be mostly used in IOS devices to identify the device.
Difference between GUID and UUID
Both uniquely identify the value. UUID
scope is universal, and GUID scope is global. GUID is the Microsoft implementation of UUID. Both generate 16 bytes of characters separated into 5 groups of hyphens.
Here is a sample Unique identifier
38bcc565-59f8-4506-818c-9d9dd7cb7b11
GUID UUID uses
- Primary keys in Database tables to uniquely identify rows
- values to identify windows registries to identify windows resources
- To identify the data spread across servers in big data
- Used in REST API for doing CRUD operations using the identifier
How to generate a UUID or GUID in javascript?
There are various ways to create a GUID
or UUID
.
This example creates UUID, and GUID in javascript.
function uuid() {
var uuidValue = "",
k,
randomValue;
for (k = 0; k < 32; k++) {
randomValue = (Math.random() * 16) | 0;
if (k == 8 || k == 12 || k == 16 || k == 20) {
uuidValue += "-";
}
uuidValue += (
k == 12 ? 4 : k == 16 ? (randomValue & 3) | 8 : randomValue
).toString(16);
}
return uuidValue;
}
console.log(uuid());
output is
c50a2fbe-99b9-4d90-b332-a1a054c946b4
Generate Unique Identifier in Nodejs with an example
npm package UUID
is available to generate a Unique identifier.
First, install using the below command.
npm install uuid
Here is an example of usage
const uuid = require("uuid/v1");
uuidv1();
Output is
6a72d3cd-2d53-4e0e-8acb-0e1c98331fe1
Typescript - generate UUID or GUID with an example
In typescript applications, You can use the npm package UUID
. first, install the uuid package using the npm install uuid command. In a typescript file, import the uuid package and directly use the uuid()
function as below.
import { v1 as uuid } from "uuid";
const id: string = uuid();
Conclusion
You learned unique identifiers - GUID UUID and how to generate in javascript and nodejs applications.