Generación de ID aleatorio en JavaScript
Vamos a crear una función que
genere una cadena con un id aleatorio:
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;
}
Utilicemos nuestra función:
let id = getId();
console.log(id);
Copie la función proporcionada y compruebe su funcionamiento.