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
JavaScript
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;
};
Usage
HTML
<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>
Try it Yourself

JavaScript Random Numbers

This function generates random numbers.

Preview
JavaScript
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;
};
Usage
HTML
<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>
Try it Yourself

JavaScript Random Letters Lowercase Only

This function generates only random lowercase letters.

Preview
JavaScript
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;
};
Usage
HTML
<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>
Try it Yourself

JavaScript Random Letter Uppercase Only

This function generates only uppercase random letters.

Preview
JavaScript
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;
};
Usage
HTML
<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>
Try it Yourself

JavaScript Random Number Between

This function generates a random number between the two numbers you provide.

Preview
JavaScript
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;
};
Usage
HTML
<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>
Try it Yourself

JavaScript Random Password

This function generates random mixed characters including numbers, uppercase, lowercase, numbers, and special characters.

Preview
JavaScript
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;
};
Usage
HTML
<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>
Try it Yourself