bluehost-banner

How to generate random digit with specific length in JavaScript

Using the below code we can generate a random numbers with specific lengths:

 function generateRandomNumber(digit) {
        const digits = '0123456789';
        let otp = '';
        for(let i = 0; i < digit; i++) {
            otp += Math.floor(Math.random() * 10);
        }
        return otp; 
    }
generateRandomNumber(6);

Will Output:

789584