Updated April 20, 2023
Introduction to JavaScript UUID
A universally unique identifier (UUID) is an identifier of the 128-bit value that is used in the construction of software. Each bit present in the value differs by the meaning as several variants are considered. The main objective of this UUID is every time the numbers are generated, the value obtained will be universally unique. That is, the UUID generated for one person won’t be the same as that of another person. Canonical formats with the text of hexadecimal type are used for human-readable display. More details on UUID will be discussed in the below sections. In this topic, we are going to learn about JavaScript UUID.
Syntax
UUID in JavaScript can be created using different methods such as math.Random(), ES6 crypto API gets a random values method. Here, the values obtained using Math. The random method may or may not be unique. So, it is always advised not to use math. random method. However, it is used for demo purposes.
According to the complaint of RFC4122 version 4, UUID should be in the format as mentioned below.
xxxxxxxx-xxxx-Mxxx-Nxxx-xxxxxxxxxxxx
Here, the allowed values of M and N are limited to 1,2,3,4, and 5.
How does UUID function work in JavaScript?
As already mentioned in the above section, UUID can be generated using different ways. Before going to that, let us see what are the basic requirements of a good identifier.
- Fast to generate- Tons of ID should be generated at a small cost.
- Uniform- It can help in the prevention of brute-force attacks in case of very sensitive data like a voucher token or prepaid token.
- Secure- This helps in avoiding collisions as UUIDs should be “unique”.
- Unpredictable- Even though this is also a part of the security feature, it is sometimes used for data integrity checks as well.
- URL-friendly- This feature can help in the cases of validation token of email in a GET parameter of URL.
- Tiny- This feature helps in shipping it in web application at a small cost.
Creation of JavaScript UUID
Now, let us move on to the creation of UUID using different methods. First, let us see how it can be done using the random() method.
1. Creating UUID in Javascript with the help of Math.Random() method:
As we know, Math.random is an in-built function in JavaScript that permits us to generate random numbers. That is, every time the user tries to run the code, a unique combination of digits will be returned. As it always returns a number of decimal type, rounding off the number should be done first. For the creation of UUID in javascript, Math.Random() function can be used as shown below.
<script>
function func() {
return ( ( ( 1+Math.random() ) * 0x10000 ) | 0 ).toString( 16 ).substring( 1 );
}
// For calling it, stitch '3' in the 3rd group
UUID = (func() + func() + "-" + func() + "-3" + func().substr(0,2) + "-" + func() + "-" + func() + func() + func()).toLowerCase();
alert(UUID);
</script>
2. Creating UUID in Javascript with the help of ES6 Crypto API
For creating a UUID, perform the below steps.
- Install the UUID using the command
npm install UUID
- Create a UUID of ES6 module syntax using the below format.
import {v4 as UUIDv4} from 'UUID' ;
UUIDv4() ;
Moreover, ES6’s crypto API of Javascript can be used to generate UUID at the client-side. In this crypto API, a method known as getRandomValues() comes up with it in order to generate a UUID( Universally Unique Identifier ) as depicted below.
function CreateUUID( ) { return ([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace( / [018]/g, c => (c ^ crypto.getRandomValues ( new Uint8Array ( 1 ))[0] & 15 >> c / 4).toString(16) )}
Examples
Let us see some sample programs on UUID generation:
Example #1
Javascript program to generate UUID using random() method.
Code:
<html>
<script>
function func() {
function ff(s) {
var pt = (Math.random().toString(16)+"000000000").substr(2,8);
return s ? "-" + pt.substr(0,4) + "-" + pt.substr(4,4) : pt ;
}
return ff() + ff(true) + ff(true) + ff();
}
var UUID = func();
alert(UUID);
</script>
</html>
Output:
In this program, the Math.random() method returns a decimal number between 0 and 1 with 16 digits. Then it is converted to a string with 16 as a base. Then prefix is cut and a string is obtained with 8 hexadecimal characters long. Appending “0000000000” to the string and cutting it with substr() function makes it 9 characters. This is done to solve the worst-case scenario where 0 and 1 is obtained as a result. On executing the code, UUID gets displayed as shown above.
Example #2
Javascript program to generate UUID using random() method and regular expression.
Code:
<html>
<script>
function func() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace( /[xy]/g , function(c) {
var rnd = Math.random()*16 |0, v = c === 'x' ? rnd : (rnd&0x3|0x8) ;
return v.toString(16);
});
}
var UUID = func();
alert(UUID);
</script>
</html>
Output:
In this program also, a function func is used which randomly creates a number using random method and regular expressions in order to create a UUID. On executing the code, it can be seen that a number is displayed as popup. If the code is executed again, another number gets displayed similar to it.
Example #3
Javascript program to generate UUID using random() method.
Code:
<script>
function func() {
return ( ( ( 1+Math.random() ) * 0x10000 ) | 0 ).toString( 16 ).substring( 1 );
}
// For calling it, stitch '3' in the 3rd group
UUID = (func() + func() + "-" + func() + "-3" + func().substr(0,2) + "-" + func() + "-" + func() + func() + func()).toLowerCase();
alert(UUID);
</script>
Output:
In this program, a function func is used which randomly creates a number using the random method in order to create a UUID. On executing the code, it can be seen that a number is displayed as popup. If the code is executed again, another number gets displayed as shown below.
Conclusion
UUID is an identifier of 128-bit value which helps in the construction of software. In this article, different aspects such as syntax, working, and examples of UUID is explained in detail.
Recommended Articles
This is a guide to JavaScript UUID. Here we discuss how does UUID function works in JavaScript along with respective programming examples. You may also have a look at the following articles to learn more –