Sometimes, we want to generate a random number that is 9 digits in length with JavaScript.
In this article, we’ll look at how to generate a random number that is 9 digits in length with JavaScript.
How to generate a random number that is 9 digits in length with JavaScript?
To generate a random number that is 9 digits in length with JavaScript, we use the Math.random
method.
For instance, we write
const n = Math.floor(100000000 + Math.random() * 900000000);
to use 100000000 + Math.random() * 900000000
to generate a number that’s 9 digits with decimal places.
Then we call Math.floor
to round it down to between 100000000 and 999999999.
Conclusion
To generate a random number that is 9 digits in length with JavaScript, we use the Math.random
method.