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 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?
To meet requirement [a-zA-Z0-9] and length=5 use
Lowercase letters, uppercase letters, and numbers will occur.
I know everyone has got it right already, but i felt like having a go at this one in the most lightweight way possible(light on code, not CPU):
It takes a bit of time to wrap your head around, but I think it really shows how awesome javascript's syntax is.
In case anyone is interested in a one-liner (although not formatted as such for your convenience) that allocates the memory at once (but note that for small strings it really does not matter) here is how to do it:
You can replace
5
by the length of the string you want. Thanks to @AriyaHidayat in this post for the solution to themap
function not working on the sparse array created byArray(5)
.Generate 10 characters long string. Length is set by parameter (default 10).
I think this will work for you: