add uuid in javascript

To generate UUID in JavaScript, we can use the uuid library that can be installed from NPM. Below are the steps to generate a UUID using the uuid library.

  1. Install the uuid library using NPM.
index.tsx
npm install uuid
17 chars
2 lines
  1. Import the uuid library in your JavaScript code.
index.tsx
const { v4: uuidv4 } = require('uuid');
40 chars
2 lines
  1. Use the uuidv4 function to generate a new UUID.
index.tsx
const uuid = uuidv4();
console.log(uuid);
42 chars
3 lines

The above code will generate a new UUID and log it to the console. The uuidv4() function generates a random UUID each time it is called.

Alternatively, if you are using ES6 or above, you can import uuid like this:

index.tsx
import { v4 as uuidv4 } from 'uuid';
37 chars
2 lines

And then use it in the same way as shown above.

gistlibby LogSnag