Generate Random String using JavaScript


In this post, we will be writing a simple JavaScript function that will generate random string of desired length. We can generate a random string with fixed set of characters using simple match and string functions.

function generateRandomString(length){
    var chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ@!#$%^&*()';
    var random_string = '';
    if(length > 0){
      for(var i=0; i < length; i++){
          random_string += chars.charAt(Math.floor(Math.random() * chars.length));
    return random_string;



Follow this video for complete guidance :

ALSO READ  Free Personality Check with DOB using JavaScript in 5 minutes

Comments are closed.