How to create a GUID / UUID in JavaScript ? Last Updated : 20 Aug, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report GUIDs (Globally Unique Identifiers) and UUIDs (Universally Unique Identifiers) are 128-bit unique identifiers used across systems to uniquely identify resources like files or objects. They are typically represented as 32-character hexadecimal strings, making them crucial for ensuring uniqueness in distributed environments.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.Syntaxxxxxxxxx-xxxx-Mxxx-Nxxx-xxxxxxxxxxxxParameters: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).ApproachUsing 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 the function generates a UUID in the 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx' format with random hex digits, a fixed '4', and a specific 'y' pattern, then logs it. 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); }); } Output8e8679e3-02b1-410b-9399-2c1e5606a971 Example 2: In this example we uses the 'uuid' library's uuidv4 function to generate and log a random UUID, simplifying UUID generation with concise code. 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 Comment More infoAdvertise with us Next Article How to Create JSON String in JavaScript? A aaliyazai3a1s Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Questions Similar Reads How to create an id using mongoose in Javascript? Mongoose is a powerful tool that simplifies MongoDB operations by providing a structured way to define schemas and models. With Mongoose, developers can define relationships between data, apply validation rules, and create reusable query logic. In this article, We will learn about Mongoose, Defining 5 min read How to Create an Image Element using JavaScript? Creating an image element dynamically using JavaScript is a useful technique when you want to add images to a webpage without having to manually write the <img> tag in your HTML. This allows for more flexibility, as we can create and manipulate images based on user interactions or other condit 2 min read How to Create JSON String in JavaScript? JSON strings are widely used for data interchange between a server and a client, or between different parts of a software system. So converting objects to JSON strings is very important for good client-server communication. Below are the following approaches to creating a JSON string: Table of Conte 2 min read How to Create Dynamic Values and Objects in JavaScript? In JavaScript, you can choose dynamic values or variable names and object names and choose to edit the variable name in the future without accessing the array, Dynamic values and objects in JavaScript allow changing, accessing, or creating values and object properties at runtime for flexibility and 3 min read How to Convert Base64 to File in JavaScript? In web development, Base64 encoding is often used to represent binary data, such as images or files, with a string of ASCII characters, sometimes you may be required to change this Base64 string back into a file for instance for file uploads, downloads, or processing in the browser, this article aim 2 min read How to Create and Manipulatinag JSON Data in javaScript? The JavaScript Object Notation (JSON) is a lightweight data interchange format commonly used for transmitting data between the server and a web application. The JSON data is represented as the key-value pairs and is easy to read and write for both humans and machines. In JavaScript, JSON objects can 3 min read Like