I think this will work for you:

function makeid(length) {
    var result           = '';
    var characters       = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
    var charactersLength = characters.length;
    for ( var i = 0; i < length; i++ ) {
        result += characters.charAt(Math.floor(Math.random() * charactersLength));
    }
    return result;
}

console.log(makeid(5));

Answer from csharptest.net on Stack Overflow
🌐
Programiz
programiz.com › javascript › examples › generate-random-strings
JavaScript Program to Generate Random String
In the above example, the Math.random() ... a random character is generated. // program to generate random strings const result = Math.random().toString(36).substring(2,7); console.log(result);...
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › random-string-generator-using-javascript
Random String Generator using JavaScript - GeeksforGeeks
August 5, 2025 - Learn how to create a random string generator using JavaScript. This guide covers generating random strings, including alphanumeric characters and passwords, with practical examples.
🌐
Sentry
sentry.io › sentry answers › javascript › generate random string/characters in javascript
Generate random string/characters in JavaScript | Sentry
The Uint8Array constructor creates a new Uint8Array array, which represents an array of 8-bit unsigned integers. The crypto.getRandomValues method populates the Uint8Array array with random numbers with a value between 0 and 255.
🌐
DEV Community
dev.to › oyetoket › fastest-way-to-generate-random-strings-in-javascript-2k5a
Javascript Random String: Fastest Way to Generate Random Strings in JavaScript - DEV Community
June 2, 2020 - Basically the idea is to use Math.random(), then you can convert it to string and do some simple string manipulation on it.
🌐
Medium
medium.com › @randomstr › generating-random-strings-in-javascript-and-its-frameworks-118cb1c9cba7
Generating Random Strings in JavaScript and Its Frameworks | by Random STR | Medium
December 1, 2023 - function generateRandomString(length) { let result = ''; const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; const charactersLength = characters.length; for ( let i = 0; i < length; i++ ) { result += characters.charAt(Math.floor(Math.random() * charactersLength)); } return result; } console.log(generateRandomString(10)); // Outputs a 10-character random string · React developers can integrate the same function within components: import React from 'react'; function MyComponent() { const randomString = generateRandomString(10); return <div>{randomString}</div>; } export default MyComponent; Node.js offers a more secure way to generate random strings using the crypto module:
🌐
Sling Academy
slingacademy.com › article › ways-to-generate-random-strings-in-javascript
4 Ways to Generate Random Strings in JavaScript - Sling Academy
February 25, 2023 - It helps us randomly select characters ... numbers) by using the ... const generateRandomString = (length) => { let result = ''; const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; const charactersLength = characters.length; for (let i = 0; i < length; ...
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › generate-random-characters-numbers-in-javascript
Generate Random Characters & Numbers in JavaScript - GeeksforGeeks
August 5, 2025 - Generate random characters and numbers in JavaScript utilizing Math.random for random numbers and String.fromCharCode() for generating random strings.
🌐
GitHub
gist.github.com › 6174 › 6062387
Generate a random string in JavaScript In a short and fast way! · GitHub
const idString = async (string_length) => { var random_str = ""; var characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZqeytrpolkadjsghfgmnbzxcvnQPOWEYRKASJHDGFMNBCVX--___-_jsfhrlg-_124903564576986483658fgh4sdfh687e4h897WETHJ68F7G4688471877GFHJFFGJ87469857468746hfghwrtiyj4598yhdjkhgnk"; for (let index = 0; index < string_length; index++) { random_str += characters.charAt( Math.floor(Math.random() * characters.length) ); } let string = `${random_str}`; console.log(string); return string; };
🌐
Vultr Docs
docs.vultr.com › javascript › examples › generate-random-string
JavaScript Program to Generate Random String | Vultr Docs
November 27, 2024 - In this article, you will learn how to write a JavaScript program to generate random strings. Explore examples that demonstrate generating strings of different lengths and compositions, ensuring you can tailor the functionality to your specific requirements.
🌐
Scaler
scaler.com › home › topics › random string generator in javascript
Random String Generator in JavaScript - Scaler Topics
January 1, 2024 - ... In this example, we use the generateRandomString function to create a random string generator javascript of length 6 by selecting characters from the ASCII range '0' to '9'. It generates random character codes within this range and converts ...
🌐
Delft Stack
delftstack.com › home › howto › javascript › javascript random string
How to Generate Random String in JavaScript | Delft Stack
March 11, 2025 - Learn how to generate random strings in JavaScript with multiple methods including Math.random, custom character sets, and cryptographic solutions. This comprehensive guide covers everything you need to create secure and flexible random strings for your applications.
🌐
Attacomsian
attacomsian.com › blog › javascript-generate-random-string
How to generate a random string in JavaScript
October 23, 2020 - The Math.random() method returns a random number between 0 (inclusive), and 1 (exclusive). You can convert this random number to a string and then remove the trailing zeros:
🌐
Javatpoint
javatpoint.com › random-string-generator-using-javascript
Random String Generator using JavaScript - javatpoint
Random String Generator using JavaScript with javascript tutorial, introduction, javascript oops, application of javascript, loop, variable, objects, map, typedarray etc.
🌐
TecAdmin
tecadmin.net › generate-random-string-in-javascript
Generate a Random String in JavaScript– TecAdmin
April 26, 2025 - In JavaScript, strings are represented by a sequence of characters between two double quotes (""). Since strings are just a sequence of characters, we can use the Math.random() method to generate a random string. Let’s look at a basic example where we construct a random string using the Math.random() method.
🌐
TutorialsPoint
tutorialspoint.com › generate-random-string-characters-in-javascript
Generate random string/characters in JavaScript?
On running the above script, the output window will pop up, displaying the random string generated of length 2 on the webpage. This is because the event gets triggered as soon as the user runs the script, and it will produce a different random string as long as the user runs the script. Let's look into the another example, where we are using the Math.random()
🌐
Media College
mediacollege.com › internet › javascript › number › random.html
How to Create Random Numbers & Characters with JavaScript
<script language="javascript" type="text/javascript"> function randomString() { var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz"; var string_length = 8; var randomstring = ''; for (var i=0; i<string_length; i++) { var rnum = Math.floor(Math.random() * chars.length); ...
🌐
Medium
medium.com › @python-javascript-php-html-css › how-to-generate-a-random-5-character-string-in-javascript-f35accc66cbd
How to Use JavaScript to Create a Random String of Five Characters
August 24, 2024 - In this article, we will explore the most effective way to generate a 5-character string using characters from the set [a-zA-Z0–9]. By the end of this guide, you’ll have a clear understanding of how to implement this functionality in your JavaScript projects. ... In the first script, we use JavaScript to generate a random 5-character string.
🌐
npm
npmjs.com › package › random-string-generator
random-string-generator - npm
March 3, 2025 - random('uppernumeric'); // '8DOUIL7RW8MW' or others · To generate upper case numeric characters. example
      » npm install random-string-generator
    
Published   Mar 03, 2025
Version   1.0.7
Author   yuhenabc