How to generate UUID without Lib in JavaScript

The Problem

Sometimes when you're working with items create, you need to generate a unique ID to your item, for some long time, we had to use libs to solve this or other options like a count or maybe the current timestamp. This is in the past.

Web Crypto API

If your website is hosted in a web security environment (https) or at localhost, you could use the Web Crypto API to generate random uuid in a very simple way:

const uuid = crypto.randomUUID()

console.log(uuid) // 'f38aa0c4-3458-41cc-9256-0145ee39d069'

If your are using node.js, you don't need a security environment, maybe you'll need to require the crypto package.

const crypto = require('crypto')

const uuid = crypto.randomUUID()

console.log(uuid) // 'f38aa0c4-3458-41cc-9Ax6-0145ee39d069'

For more details check out the mdn docs.