Random String Generator using JavaScript Last Updated : 28 Jun, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report A Random String Generator is a small program that creates a string (a set of characters) made up of random letters, numbers, and/or symbols. These random strings are useful in many places like:Creating temporary passwordsGenerating unique IDsMaking test dataCreating captcha codesApproaches to Generate Random String Below are two main methods that can be used to generate random string 1. Generating the random string from the specified length of the character set:This method involves generating a random string of a user-defined length using characters from a predefined set. We use basic JavaScript functions along with DOM manipulation to make it interactive.How it WorksMath.random() generates a random decimal between 0 and 1.Multiplying it by charactersLength gives a number between 0 and (charactersLength - 1).Math.floor() ensures the index is a whole number.charAt(index) picks the character at the randomly generated index.The character is appended to the result string. HTML <html> <head> <style> body { font-family: Arial, sans-serif; background: #f0f4f8; display: flex; flex-direction: column; align-items: center; padding-top: 60px; } h2 { color: #333; margin-bottom: 10px; } #target { font-size: 24px; color: #007bff; margin: 20px 0; height: 30px; } input[type="number"] { padding: 10px; width: 80px; font-size: 16px; border: 1px solid #ccc; border-radius: 6px; margin-right: 10px; } button { padding: 10px 20px; font-size: 16px; background-color: #007bff; color: white; border: none; border-radius: 6px; cursor: pointer; } button:hover { background-color: #0056b3; } .container { background-color: white; padding: 30px 40px; border-radius: 10px; box-shadow: 0 0 10px rgba(0, 0, 0, 0.1); text-align: center; } </style> </head> <body> <div class="container"> <h2>Random String Generator</h2> <h4 id="target"></h4> <input id="strlength" type="number" value="5" min="1" max="100" /> <button id="gen" onClick="generate()">Generate</button> </div> <script> function generate() { let length = document.getElementById("strlength").value; const characters = 'abcdefghijklmnopqrstuvwxyz'; let result = ''; const charactersLength = characters.length; for (let i = 0; i < length; i++) { result += characters.charAt(Math.floor(Math.random() * charactersLength)); } document.getElementById("target").innerText = result; } </script> </body> </html> OutputRandom String Generator2. Generate Random String using String.fromCharCode()This method creates a random string using character codes (UTF-16) with the help of JavaScript’s String.fromCharCode() function.How It WorksMath.random() generates a decimal between 0 and 1.Multiplying it by 26 gives a range from 0 to just under 26.Math.floor() rounds it down to the nearest whole number (0–25).Adding 97 shifts the range to ASCII values for 'a' to 'z'.String.fromCharCode() converts the ASCII code to its character. HTML <html> <head> <style> body { font-family: Arial, sans-serif; background: #f5f7fa; display: flex; justify-content: center; align-items: center; height: 100vh; margin: 0; } .container { background: #ffffff; padding: 30px 40px; border-radius: 10px; box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15); text-align: center; max-width: 400px; } h2 { color: #333; margin-bottom: 20px; } #target { font-size: 1.2rem; color: #007acc; margin-bottom: 20px; font-weight: bold; } input[type="number"] { padding: 8px 12px; font-size: 16px; width: 80px; margin-right: 10px; border: 1px solid #ccc; border-radius: 6px; } button { padding: 10px 20px; background-color: #007acc; color: white; font-size: 16px; border: none; border-radius: 6px; cursor: pointer; transition: background 0.3s ease; } button:hover { background-color: #005fa3; } </style> </head> <body> <div class="container"> <h2>Random String Generator</h2> <h4 id="target"></h4> <input id="strlength" type="number" value="5" min="1" max="100" /> <button id="gen" onclick="generate()">Generate</button> </div> <script> function generate() { let length = document.getElementById("strlength").value; let result = ''; for (let i = 0; i < length; i++) { result += String.fromCharCode(97 + Math.floor(Math.random() * 26)); } document.getElementById("target").innerHTML = result; } </script> </body> </html> OutputRandom String Generator Comment More infoAdvertise with us Next Article Random Password Generator Using TypeScript M meetgor Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Methods JavaScript-Questions Similar Reads Random Image Generator using JavaScript In this article, we will learn how to generate random images in fixed intervals using only HTML, CSS, and JavaScript.Approach: The pictures that are going to be generated randomly on the website should be stored in the form of an array; these pictures are then displayed to the user within a regular 4 min read How to Generate a Random Boolean using JavaScript? To generate a random boolean in JavaScript, use Math.random(), which returns a random number between 0 and 1.Approach: Using Math.random() functionCalculate Math.random() function.If it is less than 0.5, then true otherwise false.Example 1: This example implements the above approach. JavaScript// Fu 1 min read How to Generate a Random Password using JavaScript? Creating a random password generator is an excellent beginner-friendly project that can be useful in real-world applications. Passwords are vital for securing accounts, and strong, randomized passwords enhance security significantly. How to Generate a Random Password using JavaScriptWhat We're Going 5 min read Generate Random Number in Given Range Using JavaScript Here are the different ways to generate random numbers in a given range using JavaScript1. Using Math.random(): basic ApproachThis is the simplest way to generate a random number within a range using Math.random().JavaScriptlet min = 10; let max = 20; let random = Math.floor(Math.random() * (max - m 3 min read Random Password Generator Using TypeScript Generating strong and secure passwords is essential for protecting user accounts from unauthorized access. A random password generator in TypeScript ensures better security by creating unpredictable and complex passwords with a mix of letters, numbers, and special characters.What We Are Going to Cre 5 min read Random Quote Generator Using HTML, CSS and JavaScript A Random Quote Generator is capable of pulling quotes randomly from an API, a database, or simply from an array. We will be designing a Random Quote Generator from scratch using HTML, CSS, JavaScript, and type.fit API. The webpage displays a random quote from a collection and upon the click of a but 8 min read Like