Created
January 7, 2019 13:22
-
-
Save egulhan/72bbd03ca21a72175dc281f9955b6a4a to your computer and use it in GitHub Desktop.
Generate a random string using Javascript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Generates random string | |
* @param length | |
* @returns {string} | |
*/ | |
function generateRandomString(length) { | |
var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; | |
length = typeof length == 'undefined' ? 16 : length; | |
var rndCharIndex, rndStr = ""; | |
for (var i = 1; i <= length; i++) { | |
rndCharIndex = Math.floor(Math.random() * characters.length); | |
rndStr += characters.charAt(rndCharIndex); | |
} | |
return rndStr; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment