Categories
JavaScript Answers

How to generate a 5-digit number in every case with JavaScript?

Spread the love

Sometimes, we want to generate a 5-digit number in every case with JavaScript.

In this article, we’ll look at how to generate a 5-digit number in every case with JavaScript.

How to generate a 5-digit number in every case with JavaScript?

To generate a 5-digit number in every case with JavaScript, we can use the Math.random method.

For instance, we write

const min = 10000;
const max = 99999;
const num = Math.floor(Math.random() * (max - min + 1)) + min;

to call use Math.random() * (max - min + 1) to generate numbers between 0 and 89999.

And then we add min to that so that num is always a 5 digit number.

Conclusion

To generate a 5-digit number in every case with JavaScript, we can use the Math.random method.

Leave a Reply

Your email address will not be published.

If you like the content of this blog, subscribe to my email list to get exclusive articles not available to anyone else.