JavaScript random string | alpha-numeric Example

Generating Pure JavaScript random string is needed some code of logic, you can do it by using for loop, math floor, and random method.

What’s the best way to generate random string/characters in JavaScript?

Answer: Example of 5 character strings composed of characters picked randomly from the set [a-zA-Z0-9] in JS. Random alpha-numeric (uppercase, lowercase, and numbers) code:-

Output:

generate random string characters in JavaScript

How to generate Javascript generate unique string?

Answer:

You should use .length property of your string of possible characters(charset).

Also, use Math.floor a method in order to get integer positions of your chars array.

You can get a random item from charset string using its array index:

You can do same way to generating 5 char random string in JavaScript.

Output:

generate Javascript generate unique string

Do comment if you have any doubts and suggestion on this topic.

Note: The All JS Examples codes are tested on the Safari browser (Version 12.0.2) and Chrome.
OS: macOS 10.14 Mojave
Code: HTML 5 Version


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.