Generate 6 digit random number in javascript

Sometimes you have to generate the random number. You can generate 6 digit random number in javascript using random method which returns output from between two digits(min, max).

Answers 1
  • Generate 6 digit random number in javascript

    <script>
    function generateRandomNumber() {
        var minm = 100000;
        var maxm = 999999;
        return Math.floor(Math
        .random() * (maxm - minm + 1)) + minm;
    }
    let output = generateRandomNumber();
    console.log(output);
    </script>
    
    0

    Output:

    348650

    This function will generate the 6 digit random number in javascript. You can increase and decrease the number of length of mixm and maxm variable to get the specific length of random number. Like if you want to get the random number of 8 digit or 4 digit in that case you can increase/decrease the minm / maxm variable.

    Note:

    To get the 4 digits random number change minm and maxm variable value to -

    var minm = 1000;
    var maxm = 9999;

    To get the 10 digits random number change minm and maxm variable value to -

    var minm = 1000000000;
    var maxm = 9999999999;

    To get the 20 digits random number change minm and maxm variable value to -

    var minm = 10000000000000000000;
    var maxm = 99999999999999999999;
  • Back to code snippet queries related javascript