node js get random string code example
Example 1: javascript generate random string
Math.random().toString(36).substring(2);
Example 2: how to generate random string in node js
var crypto = require("crypto");
var id = crypto.randomBytes(20).toString('hex');
Example 3: random string generator node js
function makeid(length) {
var result = [];
var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
var charactersLength = characters.length;
for ( var i = 0; i < length; i++ ) {
result.push(characters.charAt(Math.floor(Math.random() *
charactersLength)));
}
return result.join('');
}
console.log(makeid(5));