Skip to content

Instantly share code, notes, and snippets.

@tobiasroeder
Created July 5, 2023 11:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tobiasroeder/bd7da01ccac0b55c0dc489376154d9f0 to your computer and use it in GitHub Desktop.
Save tobiasroeder/bd7da01ccac0b55c0dc489376154d9f0 to your computer and use it in GitHub Desktop.
Create a simple ID with JavaScript.
/**
* Create a simple ID with JavaScript.
*
* @author Tobias Röder
* @version 1.0.0
*
* @param {Number} length
*
* @returns {String}
*/
function createId(length = 8) {
const chars =
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
let id = '';
for (let i = 0; i < length; i++) {
id += chars[Math.floor(Math.random() * (chars.length - 1) + 0)];
}
return id;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment