Generate Random String using JavaScript

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.

<script>
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;
}


alert(generateRandomString(10));


</script>

Follow this video for complete guidance :

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *