I want a 5 character string composed of characters picked randomly from the set [a-zA-Z0-9]
.
What\'s the best way to do this with JavaScript?
I did not find a clean solution for supporting both lowercase and uppercase characters.
Lowercase only support:
Math.random().toString(36).substr(2, 5)
Building on that solution to support lowercase and uppercase:
Math.random().toString(36).substr(2, 5).split('').map(c => Math.random() < 0.5 ? c.toUpperCase() : c).join('');
Change the 5
in substr(2, 5)
to adjust to the length you need.