How to create GUID/UUID in JavaScript? Need a working method
Hi everyone! I'm writing a small web application in pure JavaScript (no frameworks), and I need to generate unique identifiers for records in local storage (localStorage). The problem is that users can create many records, and I want to avoid collisions (ID conflicts).
I know that in other languages (e.g., C# or Python) there are built-in functions for creating GUID/UUID. I haven't found such in JavaScript. I tried writing my own function using Math.random(), but after reading on Stack Overflow, I realized it's not safe and can produce duplicates.
Here's what I tried:
```javascript
function generateId() {
return Math.random().toString(36).substr(2, 9);
}
```
This works, but I'm not sure about uniqueness. I've seen that many recommend using the API `crypto.random
No answers yet.
Log in to leave an answer