Javascript generate random string example

In this article, I will show you how to generate random string or character in javascript. Javascript generate random string are used for various purpose in software development. If you are using Javascript-based program that needs random strings. You need javascript generate random string then this article is for you.

Why do we need to javascript generate random string?

Javascript random string are used to construct tokens, user IDs, and password in developing. Let’s say you want to make a authenticate system for a web application. You need to assign each user a unique login ID to allow them to log in. The same applies to user tokens, which are used by various systems. Random string that’s used to add additional security to login systems.

We will create a Javascript program of random string generator that will generate only alphabetic string.

Method to generate random string in javascript

This part discusses four methods that allow you to create a random string in javascript.

  • Using a custom method
  • Using Math.random() method
  • Using URNG library
  • Using built-in method
1. Using a custom method

You can build a custom method to generate a random string. Here we defined the words, numbers, and special characters. Let’ see below example.

function generateRandomString(length) {
var textValue = "";
    var characters = 'ABCDEFJHIJKLMNOPQRSTUVWXYZabcdefjhijklmnopqrstuvwxyz0123456789';
    
    for (var i = 0; i < length; i++) {
        textValue += characters.charAt(Math.floor(Math.random() * characters.length));
    }
    return textValue;
} 
generateRandomString(10);

Explanation:

  • The characters string contains uppercase, lowercase letters and digits.
  • In each iteration, Math.random() generates a random index to pick a character.
  • The function returns a random string of the specified length.
2. Using Math.random() method

The Math.random() method returns a pseudorandom number between 0 and 1. This method uses the current date and time as the source of entropy. Javascript respresented by a sequence of characters between two double quotes like this(“”). String are just a sequence of characters, we can use the Math.random() method to easily javascript generate random string. Let’ see below example. 

var randomStringValue = "";
randomStringValue += Math.random();
randomStringValue += Math.random();

console.log(randomStringValue);

This example create a random string and logs it to the javascript console. You can see in a console, the string is completely random.

3. Using URNG library

There are a few libraries available for generating random string in javascript. In such a library is known Universal Random Number Generators(URNG). The library can be installed from npm or from NPM if you are using javascript on the backend. Let’ see example below.

var randomStringValue = "";
randomStringValue += URNG.generateRandomstring(10);
console.log(randomStringValue);

In this example, we create a random string using the URNG library by passing 10 as the argument. Doing this instructs the library to return 10 characters.

4. Using built-in method

Javascript tostring(36) method converts numbers to a base-36 string, which includes digits(0-9) and letters(a-z).

// Example to generate random strings

const resultString = Math.random().toString(36).substring(2,7);
console.log(resultString);
// Using Math.random and Base 36:
console.log(Math.random().toString(36).slice(-5));

//Using new Date and Base 36:
console.log(+new Date).toString(36).slice(-5);

In the above example built-in method are used to generate random characters.

I hope this tutorial help you.

Leave a Reply

Your email address will not be published. Required fields are marked *