Code With Mark
Home
About
Resources
Contact

Easily generate random alphanumeric string in javascript

Have you ever needed to come up with a random string of characters for one of your website projects? Maybe you’re looking for a random string of characters for an api key or a random alphanumeric string for record id. Whatever the case, learn how to easily generate a random alphanumeric string in javascript.  

As you may already know, the easiest way to generate random decimal numbers in javascript is to use this line of code in your js:

Math.random()

//out put would look like
0.6631204630982128

 What if you want to get rid of "0." from the numbers, you can do that like this in your javascript code:

Math.random().toString().replace('0.', '') 

//out put would look like
6631204630982128

If you are just looking to generate a random numbers, this would do the trick for you.

If you are looking to generate random alphanumeric string in javascript, you have plenty of options. 

For example, if you just wanted to add the letter "a" to your js random string, you can do it like this:

Math.random().toString(11).replace('0.', '') 

//out put would look like
483373076a725248

The javascript function ".toString()" does accept a parameter range from 2 to 36. Numbers from 2 to 10 represent: 0-9 values and 11 to 36 represent alphabets. 

If you wanted to include alphabets (a-c), you would just change "11" to "13"

Math.random().toString(13).replace('0.', '') 

//out put would look like
1254b6c9347767c

Alphanumeric random string in javascript

To ensure you have completely random alphanumeric string without repetition in your js, I would recommend you use all the alphabets.

Math.random().toString(36).replace('0.', '') 

//out put would look like
fi8vn30c46f

You can put this in a function and call it from your javascript file.

//option 1 	
var random_id_1 = function  () 
{
  return Math.random().toString(36).substr(2);
}

//option 2
var random_id_2 = function  () 
{
  return Math.random().toString(36).replace('0.', '') ;
}

Demo
For PHP Developers

Your Next PHP Project Could Make You Money for Years

Most developers get paid once for the code they write. But the developers building real wealth use those same skills to create products that generate income over and over again.

A simple SaaS, plugin, web app, or digital product can continue bringing in customers long after it's launched.

Learn How Developers Build Monthly Income →
Beginner's Guide to node jsBeginner's Guide to node js←Previous
How To Install MongoDBHow To Install MongoDBNext→

Related Posts

  • How Google Developers Think (And Why You Should Too)
  • Add Google Sign-In in 2 Minutes
  • Form Validation in 1 Line

Top Posts Viewed

Using JavaScript Window Onload Event Correctly
355 views
Learn To Create YouTube Video Downloader
314 views
How Google Developers Think (And Why You Should Too)
313 views

Categories

Courses
Excel
Google Script
Javascript
jQuery
Microsoft Access
MongoDB
Node JS
PHP
Quick Tip
Uncategorized
Wordpress