Created
July 5, 2023 11:31
-
-
Save tobiasroeder/bd7da01ccac0b55c0dc489376154d9f0 to your computer and use it in GitHub Desktop.
Create a simple ID with 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
/** | |
* 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