DEV Community

Discussion on: Making random ID with Javascript

Collapse
 
sanampakuwal profile image
Sanam

Best solution:
Date.now(); // parse this as string

Collapse
 
lionelrowe profile image
lionel-rowe • Edited

Nope, that's a really bad solution.

class User {
    constructor(name) {
        this.name = name
        this.id = String(Date.now())
    }
}

const alice = new User('Alice')
const bob = new User('Bob')

alice.id === bob.id // true
Enter fullscreen mode Exit fullscreen mode