Skip to content
Home » How to Generate Random String using JavaScript

How to Generate Random String using JavaScript

In this tutorial let’s learn about How to Generate Random String using JavaScript. This lesson will teach you how to make a random string generator using several ways. Here we will construct a random string generator program using the JavaScript programming language. It will produce a new string each time. We’ll utilize JavaScript’s Math.random() function for this.

Code to Generate Random Strings in JavaScript

To generate a random integer between 0 and 1, we utilise the Math.random() method. Math.floor(Math.random() * charactersLength) will generate a random number between 0 and the specified array length.

The for loop is used to generate the required length of the random string. Each iteration generates a new character at random.

Code Example :

const characters ='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';

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

    return result;
}

console.log(generateString(10));

Output :

 qe43ljSNb0

The below example shows how to generate a random number for a character of a given length.

Code:

<!DOCTYPE html>
<html lang="en">

<head>
	<meta charset="UTF-8">
	<title>Random String Generator</title>
</head>

<body>
	<h2>Random String Generator</h2>
	<h4 id="target"></h4>
	<input id="strlength" type="number" value="10" min="1" max="100" />
	<button id="gen" onClick="generate()">Generate</button>
	
	<script>
	function generate() {
		let length = document.getElementById("strlength").value;
		const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
		let result = ' ';
		const charactersLength = characters.length;
		for(let i = 0; i < length; i++) {
			result +=
			characters.charAt(Math.floor(Math.random() * charactersLength));
		}
		document.getElementById("target").innerHTML = result
	}
	</script>
</body>

</html>

Output:

How to Generate Random String using JavaScript

Generate Random Strings in JavaScript Using Built-in Methods

Built-in methods are utilized to generate random characters in the example below. The random number between 0 and 1 is generated by the Math.random() method. 36 represents base 36 in the toString(36) function. toString(36) uses letters to represent digits greater than 9.

Code Example :

const  generateRandomString = (num) => {
    const characters ='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
    let result1= Math.random().toString(36).substring(2,num);       

    return result1;
}

length = 10
console.log(generateRandomString(length+2));

Output :

t1a5d9td3a

To generate a random string, we can utilize the following built-in functions:

  • To produce random characters, use the Math.random() function.
  • The toString(36) method represents the characters [a-zA-Z0-9].
  • The substring(startIndex, endIndex) method returns the number of characters supplied.

Conclusion

In this post we learnt different ways to Generate Random String using JavaScript. Using normal for loop and also using the built-in methods we generated the random strings.

Similar Posts:

How to Convert a String to Boolean in JavaScript
Capitalize the First Letter of a String in JavaScript