clock  Mon - Sun 8.00 AM - 8.00 PM
mail  edu@teknowize.com
fb
instagram
play store
pinterest

How To Generate Random String/Characters In JavaScript.

  Sumit Dey Sarkar
  26 Aug 2022
  JavaScript
Imperial Advance Hero Image

How To Generate Random String/Characters In JavaScript.

In this article we will see how to generate random string/characters in JavaScript.

Here we will pass length of string and we will also use Math.random().

Let's see example -

How to generate random string/characters in JavaScript

<html>

<head>
  <title>How to generate random string/characters in JavaScript</title>
</head>

<body>
  <button type="button" onClick="alert(randomString(10))">Create Random String</button>
</body>

<script type="text/javascript">

  function randomString(length) {
    var text = "";
    var possibleChar = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

    for (var i = 0; i < length; i++)
      text += possibleChar.charAt(Math.floor(Math.random() * possibleChar.length));

    return text;
  }

</script>