JavaScript Random Strings, Random Number
JavaScript generate random strings, javaScript random number random characters functions
JavaScript Random Strings And Numbers
This function creates a random mixed string, including uppercase, lowercase, and number.
Preview
const randomStrings = (length = 10) => {
let result = "";
let chars = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
for (let i = 0; i < length; i++) {
result += chars[Math.floor(Math.random() * chars.length)];
}
return result;
};
<input type="button" onclick="getRandomStrings()" value="random text">
<div id="random-strings-mixed"></div>
<script>
const getRandomStrings = () => {
let result = randomStrings(10);
document.getElementById("random-strings-mixed").innerText = result;
};
</script>
JavaScript Random Numbers
This function generates random numbers.
Preview
const randomNumbers = (length = 0) => {
let result = "";
let chars = "1234567890";
for (let i = 0; i < length; i++) {
result += chars[Math.floor(Math.random() * chars.length)];
}
return result;
};
<input type="button" onclick="getRandomNumbers()" value="random numbers">
<div id="random-number"></div>
<script>
const getRandomNumbers = () => {
let result = randomNumbers(10);
document.getElementById("random-number").innerText = result;
};
</script>
JavaScript Random Letters Lowercase Only
This function generates only random lowercase letters.
Preview
const randomLowercase = (length = 20) => {
let result = "";
let chars = "abcdefghijklmnopqrstuvwxyz";
for (var i = 0; i < length; i++) {
result += chars[Math.floor(Math.random() * chars.length)];
}
return result;
};
<button type="button" onclick="getRandomLowercase()">Random lowercase</button>
<div id="random-lowercase"></div>
<script>
const getRandomUppercase = () => {
let result = randomLowercase(20);
document.getElementById("random-lowercase").innerText = result;
};
</script>
JavaScript Random Letter Uppercase Only
This function generates only uppercase random letters.
Preview
const randomUppercase = (length = 0) => {
let result = "";
let chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
for (let i = 0; i < length; i++) {
result += chars[Math.floor(Math.random() * chars.length)];
}
return result;
};
<button type="button" onclick="getRandomUppercase()">Random uppercase</button>
<div id="random-uppercase"></div>
<script>
const getRandomUppercase = () => {
let result = randomUppercase(20);
document.getElementById("random-uppercase").innerText = result;
};
</script>
JavaScript Random Number Between
This function generates a random number between the two numbers you provide.
Preview
const numberBetween = (min_number = 100, max_number = 500) => {
let min = Math.ceil(min_number);
let max = Math.floor(max_number + 1);
let result = Math.floor(Math.random() * (max - min)) + min;
return result;
};
<button type="button" onclick="getNumberBetween()">Between number</button>
<div id="between-number"></div>
<script>
const getNumberBetween = () => {
let result = numberBetween(1000, 2000);
document.getElementById("between-number").innerText = result;
};
</script>
JavaScript Random Password
This function generates random mixed characters including numbers, uppercase, lowercase, numbers, and special characters.
Preview
const randomPassword = (length = 8) => {
let result = "";
let chars = "1234567890";
chars += "abcdefghijklmnopqrstuvwxyz";
chars += "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
chars += "~!@#$%^&*()_+}{></[]|-,:;'\".?";
for (var i = 0; i < length; i++) {
result += chars[Math.floor(Math.random() * chars.length)];
}
return result;
};
<button type="button" onclick="getRandomPAssword()">random password</button>
<div id="random-password"></div>
<script>
const getRandomPAssword = () => {
let result = randomPassword(10);
document.getElementById("random-password").innerText = result;
};
</script>