Different ways to generate GUID in javascript

In this blog post, We are going to learn how to generate Unique Identifiers -UUID, GUID, UDID in javascript/typescript/nodejs.

UUID, GUID, UDID  examples with javascript typescript

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



  • 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;  

output is


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');  

Output is


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();  


You learned unique identifiers - GUID UUID and how to generate in javascript and nodejs applications.

Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
You'll get a notification every time a post gets published here.