3 Ways to generate random string/characters in JavaScript

In this article, we will learn How to generate random strings/characters in JavaScript.

There are so many ways by which we can generate random string and numbers in JavaScript . You can code the entire function by yourself or you can just use the in-built methods provided in JavaScript.

Here we will learn three different ways to generate strings or characters in JavaScript. But before going through the example it would be better if you have the basic knowledge of Javascript String and Math.random() function.

The Math.random() function returns a floating-point, pseudo-random number between 0 and 1 (inclusive of 0, but not 1) with uniform distribution over range which you can scale to your desired range.

Example 1 : Using Math.random()

Here we will generate random string of any size by picking characters randomly from A-Z,a-z, 0-9.

const characters ='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';

function generateString(length) {
    let randomString = ' ';
    const charactersLength = characters.length;
    for ( let i = 0; i < length; i++ ) {
        randomString += characters.charAt(Math.floor(Math.random() * charactersLength));

    return randomString;




We have declared a function generateString(length), with length as an argument that we can pass.

The for loop run for the specific number of time as passed in the generateString() function.

charactersLength : We get the length of the characters string.

Math.random() : It generates the random numbers, which will give us a random single character till the loop run.

Math.random() * charactersLength : It defines the range of the random number, so that the random number does not exceeds the characters (A-Z,a-z,0-9) string's length.

Example 2: Genearte Random String Using in-built methods

const result = Math.random().toString(36).substring(2,7);
console.log('Random String is:', result);


Random String is: fqm48

Here, we are using all the inbuilt methods of JavaScript to generate random strings.

Math.random() : Generate random numbers between 0 and 1.

toString() : It has param called radix (base), that we can pass in number between 2 to 36 and this will cast to generate number to the radix characters that fall between the given number.

In the example, we use toString(36) because we want to include all the alphanumeric characters in the alphabet.

substring() : This method is use to extract characters between two positions from a string and return as substring.

In the example, we are using substring(2,7) because we want only 5 character from the string. 2 is the start index and 7 in the end index of the string.

Example 3 : Using nanoid NPM module

In you want to generate random strings or characters very quickly and easily, then nanoid is the best package for it. It is the newer version of shortid module.

It lets you generate random characters with just one line of code.

Just install nanoid .

npm i nanoid

Then use in your JavaScript project, by importing it.

const { nanoid } = require('nanoid');
nanoid(5); //=> "4E36I"

The above examples are few ways by which you can easily generate random characters or numbers in your JavaScript projects.

Related Topics:

How to sort an array alphabetically in javascript?

How to repeat a string n number of times in JavaScript

💾 Recommended Articles