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 = '[email protected]!#$%^&*()';
    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 :



Recommended For You

About the Author: Admin