JavaScript generates a unique id | 3 Ways with examples code

How to Generate Unique ID in JavaScript?

Using UUID module or Math.random method or Date.now method can generate a unique identifier (id) in JavaScript.

JavaScript generates a unique id Examples

Let’s see all ways example in HTML and JS:-

Using UUID library

To create a random UUID…

1. Install

2. Create a UUID (ES6 module syntax)

… or using CommonJS syntax:

Read more: https://www.npmjs.com/package/uuid

Another solution

Output:

JavaScript generates a unique id UUID

Math.random built-in function

This example will return a unique combination of numbers.

Output: 19

Date.now another JavaScript built-in function

Using this way will return number of miliseconds elapsed since January 1, 1970.

Output:

Do comment if you have any questions or suggestion on this topic.

Note: All JS Examples codes are tested on the Firefox browser and the Chrome browser.
OS: Windows 10
Code: HTML 5 Version


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.