Horje
How to create a GUID / UUID in JavaScript ?

Ever heard of GUID (Globally Unique Identifier) or UUID (Universally Unique Identifier)? These are 128-bit unique identifiers used in computer systems to distinguish resources like files, objects, and components. Generated randomly, GUIDs are extremely unlikely to be duplicated. They find applications in databases, web apps, and operating systems.

Typically, GUIDs are represented as strings of 32 hexadecimal digits, for instance, “550e8400-e29b-11d4-a716-446655440000”. The generation process involves a mix of timestamps, random numbers, and network address data.

Syntax:

xxxxxxxx-xxxx-Mxxx-Nxxx-xxxxxxxxxxxx

Parameters:

  • 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).

Approach

  • 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: In this example, a concise JavaScript function generates a random UUID following the ‘xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx’ pattern. The UUID includes randomly generated hexadecimal digits, a fixed ‘4’ for version indication, and a digit following a specific pattern denoted by ‘y’. The function then prints the generated UUID to the console using console.log(random_uuid).

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
8e8679e3-02b1-410b-9399-2c1e5606a971

Example 2: In this example, a succinct JavaScript code snippet utilizes the ‘uuid’ library to generate a random UUID. The uuidv4 function from the library is assigned to random_uuid, and the generated UUID is printed to the console with console.log(random_uuid). The ‘uuid’ library simplifies the process of UUID generation in a concise manner.

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



Reffered: https://www.geeksforgeeks.org


JavaScript

Related
Why does preventDefault() on a parent element's click 'disable' a checkbox ? Why does preventDefault() on a parent element's click 'disable' a checkbox ?
Left Shift Assignment (<<=) Operator in JavaScript Left Shift Assignment (<<=) Operator in JavaScript
Right Shift Assignment(>>=) Operator in JavaScript Right Shift Assignment(>>=) Operator in JavaScript
Explain createElement() method Arguments Explain createElement() method Arguments
How to get dynamic access to an object property in JavaScript ? How to get dynamic access to an object property in JavaScript ?

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
13