10 examples of 'javascript random alphanumeric string' in JavaScript

Every line of 'javascript random alphanumeric string' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
131export function generateRandomAlphaNumericString(length: number, upToLength: boolean = false): string {
132 const characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
133 let result = "";
134 if (upToLength) {
135 length = Math.floor(Math.random() * length) + 1;
136 }
137 for (let i = 0; i < length; i++) {
138 result += characters.charAt(Math.floor(Math.random() * characters.length));
139 }
140 return result;
141}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

19function randomString() {
20 const length = (Math.floor(Math.random() * 25) + 10);
21 let str = '';
22 for (let i = 0; i < length; i += 1) {
23 str += String.fromCharCode(Math.floor(Math.random() * 255))
24 }
25 return str;
26}
5function randomString() {
6 return Math.random()
7 .toString(36)
8 .substring(7)
9}
1export default function randomString() {
2 // Return a string of ten digits.
3 return Math.random().toString().substring(2, 12)
4}
13export function randomString(): string {
14 const nbCar = getRandomInt(1, 20);
15 const cars = [];
16 for (let i = 0; i < nbCar; i++) {
17 cars.push(String.fromCharCode(65 + getRandomInt(0, 26)));
18 }
19 return cars.join("");
20}
42public static stringRandom(chars: number): string{
43 let text = "";
44 let possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
45
46 for( let i = 0; i < chars; i++ )
47 text += possible.charAt(Math.floor(Math.random() * possible.length));
48
49 return text;
50}
20function randomString() {
21 var text = "";
22 var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
23
24 for (var i = 0; i < 2; i++)
25 text += possible.charAt(Math.floor(Math.random() * possible.length));
26
27 return text;
28}
29export function randomString () {
30 var string = ''
31 var possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
32
33 for (var i = 0; i < 5; i++) { string += possible.charAt(Math.floor(Math.random() * possible.length)) }
34
35 return string
36}
84export function randomString(length = 8) {
85 return [...Array(length)].map(() => (~~(Math.random() * 36)).toString(36)).join("")
86}
130randomString() {
131 return lipsum[Math.floor(Math.random() * 100)]
132}

Related snippets