DEV Community

Cover image for How to generate "uuid" in JavaScript ?
napoukine
napoukine

Posted on

How to generate "uuid" in JavaScript ?

const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
/**
 * Generate any string and use it everywhere
 * @param {int} the length of the string you zqnt to generate
 * @returns string
 */
function VanillaGenerate(length) {
    let result = '';
    const charactersLength = characters.length;
    for (let i = 0; i < length; i++) {
        result += characters[Math.floor(Math.random() * charactersLength)];
    }
    return result;
}
/**
 * Exemple
 */
let uuid = VanillaGenerate(21);
console.log(uuid);
Enter fullscreen mode Exit fullscreen mode

Top comments (0)