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
🌐
Sentry
sentry.io › sentry answers › javascript › generate random string/characters in javascript
Generate random string/characters in JavaScript | Sentry
This function takes in the length of the random string that you want as an argument and then creates a string output where each character is randomly chosen from the chars variable. In this case, the possible characters in the random string are alphanumeric. Random characters are chosen using Math.random().
Discussions

Random string with javascript
Hi, I found a javascript code for a random string, i’d like to use. But somehow it doesn’t work in a code-node. What do I have to do/change? Thank you in advance Martin More on community.n8n.io
🌐 community.n8n.io
0
March 4, 2023
How do you generate random strings or characters in JavaScript? - LambdaTest Community
How do you generate random strings or characters in JavaScript? More on community.lambdatest.com
🌐 community.lambdatest.com
0
April 3, 2024
Random String
Add the strings to an array const words = ['jon','jack','jill'] Math.random() generates a random number between 0-1 in Javascript. Math.floor() rounds a number down to an interger. let randomIndex = Math.floor(Math.random()*words.length + 1) Then access the string from the array, based on the random index. console.log(words[randomIndex]) More on reddit.com
🌐 r/learnjavascript
9
1
December 21, 2021
Testing an action creator that generates a random string in React/Redux app?
expect(typeof updateString().newString).toBe('string') you could extract generator.generateString() out and put it somewhere else, passing in newString as a parameter, but whatever calls generator.generateString() will have the same exact problem. More on reddit.com
🌐 r/javascript
4
1
April 24, 2017
People also ask

How can I generate a random string with specific characters only?
You can define your custom character set and use it as an argument in the `generateRandomString` function.
🌐
scaler.com
scaler.com › home › topics › random string generator in javascript
Random String Generator in JavaScript - Scaler Topics
What's the maximum length of a random string I can generate using these methods?
The maximum length of a random string depends on factors like the character set and the JavaScript environment. In practice, you can generate strings of several thousand characters, but it's essential to consider performance and memory constraints.
🌐
scaler.com
scaler.com › home › topics › random string generator in javascript
Random String Generator in JavaScript - Scaler Topics
Is there a way to generate cryptographically secure random strings?
Yes, you can use the `crypto.getRandomValues` method or libraries like `crypto-random-string` to generate secure random strings.
🌐
scaler.com
scaler.com › home › topics › random string generator in javascript
Random String Generator in JavaScript - Scaler Topics
🌐
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.
🌐
Programiz
programiz.com › javascript › examples › generate-random-strings
JavaScript Program to Generate Random String
To understand this example, you should have the knowledge of the following JavaScript programming topics: ... // program to generate random strings // declare all characters 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(5));
🌐
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.
Find elsewhere
🌐
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.
🌐
Zipy
zipy.ai › blog › generate-random-string-characters-in-javascript
generate random string characters in javascript
April 12, 2024 - At the heart of random string generation in JavaScript is the Math.random() function.
🌐
GitHub
gist.github.com › 6174 › 6062387
Generate a random string in JavaScript In a short and fast way! · GitHub
const generateID = (stringLength = 20) => { let randomStr = ""; const characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZqeytrpolkadjsghfgmnbzxcvnQPOWEYRKASJHDGFMNBCVX--___-_jsfhrlg-_124903564576986483658fgh4sdfh687e4h897WETHJ68F7G4688471877GFHJFFGJ87469857468746hfghwrtiyj4598yhdjkhgnk"; for (let index = 0; index < stringLength; index++) { randomStr += characters.charAt( Math.floor(Math.random() * characters.length) ); } return randomStr; };
🌐
Vultr Docs
docs.vultr.com › javascript › examples › generate-random-string
JavaScript Program to Generate Random String | Vultr Docs
November 27, 2024 - Generating random strings in JavaScript is straightforward with the use of Math.random() and a basic loop mechanism. By customizing the function, you can adapt the random string generator for various purposes, including testing, unique identifiers, or security features in web applications.
🌐
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 ...
🌐
n8n
community.n8n.io › questions
Random string with javascript - Questions - n8n Community
March 4, 2023 - Hi, I found a javascript code for a random string, i’d like to use. But somehow it doesn’t work in a code-node. What do I have to do/change? Thank you in advance Martin
🌐
Adobe Support Community
community.adobe.com › home › app communities › captivate › questions › generate random string via javascript
Generate random string via Javascript
September 26, 2017 - I have seen some Javascript that seems to work but not sure if getting it to work in Captivate is different as I have not had success. ... Appreciate the time! ... This topic has been closed for replies. ... more variants how to generate random string in js: http://javascript-benchmark.info/t/generate-random-string
🌐
RANDOM.ORG
random.org › strings
RANDOM.ORG - String Generator
This form allows you to generate random text strings.
🌐
npm
npmjs.com › package › random-string-generator
random-string-generator - npm
March 3, 2025 - random('lowernumeric'); // 'k0oooqv77wrq' or others
      » npm install random-string-generator
    
Published   Mar 03, 2025
Version   1.0.7
Author   yuhenabc
🌐
Envato Tuts+
code.tutsplus.com › home › javascript
Generate Random Numbers and Strings in JavaScript | Envato Tuts+
January 31, 2023 - In this tutorial, we learned how to generate random numbers and alphanumeric strings in JavaScript. Generating random integers is easy in JavaScript with the help of the Math.random() method. All we had to do was scale the output so that it ...
🌐
C# Corner
c-sharpcorner.com › blogs › create-random-strings-using-javascript
Create Random Strings Using JavaScript
November 18, 2023 - <h2>Random String In JavaScript</h2> ... <script> function random() { let result = ' '; for (let i = 0; i < 6; i++) { result += String.fromCharCode(97 + Math.floor(Math.random() * 26)); } document.getElementById("random").innerHTML = result; } ...
🌐
W3Schools
w3schools.com › js › js_random.asp
JavaScript Random
As you can see from the examples above, it might be a good idea to create a proper random function to use for all random integer purposes. This JavaScript function always returns a random integer between min (included) and max (excluded):
🌐
Attacomsian
attacomsian.com › blog › javascript-generate-random-string
How to generate a random string in JavaScript
October 23, 2020 - To generate a fully random string, you should pass 16 or greater as a radix value to toString():
🌐
QuickRef.ME
quickref.me › home › how to generate a random string from given characters in javascript - quickref.me
How to generate a random string from given characters in JavaScript - QuickRef.ME
In this Article we will go through how to generate a random string from given characters only using single line of code in JavaScript. This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => Arrow Function.