programming-examples/js/String/Get unique guid of the specified length, or 32 by default.js

15 lines
529 B
JavaScript
Raw Normal View History

2019-11-15 12:59:38 +01:00
function guid(len) {
var buf = [],
chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789',
charlen = chars.length,
length = len || 32;
for (var i = 0; i < length; i++) {
buf[i] = chars.charAt(Math.floor(Math.random() * charlen));
}
return buf.join('');
}
console.log(guid());
console.log(guid(15));