Geração de ID aleatório em JavaScript
Vamos criar uma função que irá
gerar uma string com um id aleatório:
function getId(length = 16) {
let chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
let res = '';
for (let i = 0; i < length; i++) {
res += chars[Math.floor(Math.random() * chars.length)];
}
return res;
}
Vamos usar nossa função:
let id = getId();
console.log(id);
Copie a função fornecida e verifique seu funcionamento.