Generating a Random id in JavaScript
Let's create a function that will
generate a string with a random id:
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;
}
Let's use our function:
let id = getId();
console.log(id);
Copy the provided function and test its operation.