Wednesday, July 3, 2024
HomeLanguagesJavascriptHow to create a GUID / UUID in JavaScript ?

How to create a GUID / UUID in JavaScript ?

A GUID (Globally Unique Identifier) or UUID (Universally Unique Identifier) is a 128-bit unique identifier that is used in computer systems to identify resources such as files, objects, and components. A GUID is generated randomly and is very unlikely to be duplicated. It is used in various applications and systems such as databases, web applications, and operating systems.

It is typically represented as a string of 32 hexadecimal digits, such as “550e8400-e29b-11d4-a716-446655440000”. GUIDs are generated using a combination of timestamps, random numbers, and network address information.

Syntax: The basic syntax of a GUID/UUID is as follows:

xxxxxxxx-xxxx-Mxxx-Nxxx-xxxxxxxxxxxx

 

Where:

  • x – represents a hexadecimal digit (0-9, A-F).
  • M – represents the version of the GUID/UUID (1-5).
  • N – represents the variant of the GUID/UUID (8, 9, A, or B).

Approaches: There are several approaches to generating a GUID/UUID:

  • Using a programming language: Many programming languages have built-in functions or libraries to generate GUIDs/UUIDs. For example, in C#, you can use the Guid.NewGuid() method.
  • Using an online tool: There are many online GUID/UUID generators that can be used to generate a GUID/UUID. These tools are typically free and require no installation.
  • Using a command-line tool: Many operating systems have built-in command-line tools that can be used to generate GUIDs/UUIDs. For example, on Windows, you can use the guidgen.exe tool.

Example 1:

  • We’re using the built-in Math.random() function to generate random values for each of the digits in the UUID. 
  • The uuidv4() function uses the replace() method to replace the “x” and “y” characters in the UUID format string with random hexadecimal digits, with the “y” character set to one of the predefined values that ensure the UUID is a version 4 UUID.
  •  Finally, we’re printing the generated UUID to the console using console.log().

Javascript




// Generate a random UUID
const random_uuid = uuidv4();
  
// Print the UUID
console.log(random_uuid);
  
function uuidv4() {
    return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'
    .replace(/[xy]/g, function (c) {
        const r = Math.random() * 16 | 0, 
            v = c == 'x' ? r : (r & 0x3 | 0x8);
        return v.toString(16);
    });
}


Output

486ca60b-8c2c-411e-b54e-67c74a4fb925

Example 2: 

  • In this example, we’re using the uuid npm package which provides a simple and convenient way to generate UUIDs. 
  • The v4 method of the package generates a random version 4 UUID, which we’re storing in the random_uuid variable. 
  • Finally, we’re printing the generated UUID to the console using console.log(). Note that you’ll need to install the uuid package using npm before running this example.

Javascript




const { v4: uuidv4 } = require('uuid');
  
// Generate a random UUID
const random_uuid = uuidv4();
  
// Print the UUID
console.log(random_uuid);


Output:

93243b0e-6fbf-4a68-a6c1-6da4b4e3c3e4

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

Calisto Chipfumbu
Calisto Chipfumbuhttp://cchipfumbu@gmail.com
I have 5 years' worth of experience in the IT industry, primarily focused on Linux and Database administration. In those years, apart from learning significant technical knowledge, I also became comfortable working in a professional team and adapting to my environment, as I switched through 3 roles in that time.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments