There are so many ways to generate random strings in JavaScript and it doesn't really matter which method is faster.
The method I like using most...
For further actions, you may consider blocking this person and/or reporting abuse
Nice function! But one thing I noticed is that this will only work for random alpha-numeric strings up to a certain length. If you want to have something longer, you should call the method recursively. Something like:
Yep, looks like it tops out at 12-13 characters for me, demo here: jsfiddle.net/mberwk8a/2/
Hi, thanks for the simple, straight-forward article! I just wanted to point out a small error in your one-liner. You define the
lengthvariable, but you still used a hard-coded "6" instead of it in the expression.Don't use the
.substr(2, ...)at the end, because you risk having empty string as a result. The result ofMath.random()can be0, so if yousubstr(2, ...), you will end up with an empty string.Do you like base20 for something special?
No reason actually, I just like using it.
Just to clarify for people who don't know, the radix argument for
toStringgoes from 2 to 36, and you need to use 36 to include all alphanumeric characters in the alphabet. Using 20 will omit more than half of the alphabetic letters from the output.