Skip to content
Advertisement

Random alpha-numeric string in JavaScript?

What’s the shortest way (within reason) to generate a random alpha-numeric (uppercase, lowercase, and numbers) string in JavaScript to use as a probably-unique identifier?

Advertisement

Answer

If you only want to allow specific characters, you could also do it like this:

JavaScript

Here’s a jsfiddle to demonstrate: http://jsfiddle.net/wSQBx/

Another way to do it could be to use a special string that tells the function what types of characters to use. You could do that like this:

JavaScript

Fiddle: http://jsfiddle.net/wSQBx/2/

Alternatively, to use the base36 method as described below you could do something like this:

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement