Skip to content

Instantly share code, notes, and snippets.

@web2ls
Created March 22, 2022 05:26
Show Gist options
  • Select an option

  • Save web2ls/7538fa14e917de159a9f5720fca38f27 to your computer and use it in GitHub Desktop.

Select an option

Save web2ls/7538fa14e917de159a9f5720fca38f27 to your computer and use it in GitHub Desktop.
Generate random string
const randomString = (len) => {
const chars = 'abcdefhijkmnprstwxyz123456789ABCDEFGHJKMNPQRSTWXYZ';
const totalLength = chars.length;
let randomStr = '';
for (let i = 0; i < len; i += 1) {
randomStr += chars.charAt(Math.floor(Math.random() * totalLength));
}
return randomStr;
};
// 🎉 'k4RrA5'
console.log(randomString(6));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment