How to Generate a Random String in JavaScript
Table of Contents
For one reason or another, you might be wondering how to generate a random string in JavaScript.
Thankfully, there are many different ways to approach this problem.
In this post, we will be looking at how to generate a random string with binary characters, hexadecimal characters, and then one where you can specify the characters you want to use.
Generating a random string with binary characters
In all of the examples, we will take advantage of the Math.random()
function. This function returns a random number between 0 and 1.
From there, we can convert it to a string:
JAVASCRIPTconst randomNumber = Math.random().toString(2);
console.log(randomNumber);
BASH0.1101011011110101010110110001101111110110100100110001
Now we can use the substr()
method to skip the first two characters and cut off the number at a length of 8
:
JAVASCRIPTconst randomNumber = Math.random().toString(2);
const randomString = randomNumber.substr(2, 8);
console.log(randomString);
BASH00001101
Here is a re-usable function:
JAVASCRIPTconst generateRandomBinary = (length) => {
const randomNumber = Math.random().toString(2);
return randomNumber.substr(2, length);
};
BASH0110
Generating a random string with hexadecimal characters
For this, we will reuse the previous code, except change the radix of the toString()
method.
This will return a hexadecimal number instead of a binary one:
JAVASCRIPTconst randomNumber = Math.random().toString(16);
const randomString = randomNumber.substr(2, 8);
console.log(randomString);
BASH69ee0964
Here is a re-usable function:
JAVASCRIPTconst generateRandomHex = (length) => {
const randomNumber = Math.random().toString(16);
return randomNumber.substr(2, length);
};
BASHc142
Generating a random string with a custom character set
For this, we will need to use a different implementation.
First, let's define the set of characters we want to use:
JAVASCRIPTconst characters = "abcdefghijklmnopqrstuvwxyz0123456789";
Now let's select from the set a random character:
JAVASCRIPTconst randomCharacter = characters[Math.floor(Math.random() * characters.length)];
Now, let's make a method that takes in a length and returns a random string of that length:
JAVASCRIPTconst characters = "abcdefghijklmnopqrstuvwxyz0123456789";
const generateRandomString = (length) => {
let randomString = "";
for (let i = 0; i < length; i++) {
randomString += characters[Math.floor(Math.random() * characters.length)];
}
return randomString;
};
console.log(generateRandomString(8));
BASH6vudynt3
Conclusion
In this post, we took a look at how to randomly generate binary, hexadecimal, and custom strings.
From here, you can use for anything ranging from hashing to generating seeds.
Thanks for reading!
- Getting Started with Solid
- How to Serve Static Files with Nginx and Docker
- How to deploy a PHP app using Docker
- How to deploy a Deno app using Docker
- How to deploy a Node app using Docker
- Getting Started with Sass
- Using Puppeteer and Jest for End-to-End Testing
- Getting Started with Handlebars.js
- Setting Up Stylus CSS Preprocessor
- Getting Started with Vuex: Managing State in Vue
- Setting Up a Local Web Server using Node.js
- How To Create a Modal Popup Box with CSS and JavaScript