generate random characters in javascript code example
Example 1: javascript generate random string
Math.random().toString(36).substring(2);
Example 2: generate random character in javascript
var characters = 'abcdefghijklmnopqrstuvwxyz0123456789';
var result = ""
var chaactersLength = characters.length;
for ( var i = 0; i < 5 ; i++ ) {
result += characters.charAt(Math.floor(Math.random() * chaactersLength));
}
console.log(result)
Example 3: javascript get random string
function getRandomString(length) {
var randomChars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
var result = '';
for ( var i = 0; i < length; i++ ) {
result += randomChars.charAt(Math.floor(Math.random() * randomChars.length));
}
return result;
}
Example 4: random alphabet javascript
Math.random().toString(36).substr(2, 5);
Example 5: javascript random char
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
function generateRandomCode(){
let result = ""
let charactersLength = characters.length;
for ( var i = 0; i < 6 ; i++ ) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result
}