How to Create a UUID in JavaScript


The UUID identifier has been used in programming since the days when a baby with the face David Walsh became a professional software engineer. My first exposure to UUIDs was via a ColdFusion app I inherited and … the less we say about it, the better. In any case, I was recently surprised to see that JavaScript has the ability to create UUIDs.

Developers can use the built-in JavaScript crypto library to generate a UUID:

crypto.randomUUID() // '5872aded-d613-410e-841f-a681a6aa8d8b'
crypto.randomUUID() // 'fe6c7438-a833-4c7c-9ea3-cdc84ef41dfc'
crypto.randomUUID() // 'e47a03d4-5da3-4451-a2c1-265de99cc2c1'
crypto.randomUUID() // '04cdadeb-0228-43db-85dc-ce7e960a6cde'

It is important to remember that UUID is not guaranteed to be unique, although the probability of recurrence is incredibly low. I look forward to exploring window.crypto API further to see what other cool things we can do!

Monitoring site performance
Monitoring site performance
Monitoring site performance
  • To be a Dev father

    To be a Dev father

    I get asked lots of questions every day, but I’m always surprised that they are rarely questions about code or even technology — many of the questions I get are more about non-dev things like how my office is, what software I use, and often …

  • 7 important JavaScript functions

    7 important JavaScript functions

    I remember the early days of JavaScript, where you needed a simple function for pretty much everything, because browser providers implemented functions differently, and not just edge functions, basic functions such as. addEventListener and attachEvent. Times have changed, but there are still a few features that every developer should …

  • Elegant Overflow with CSS Ellipsis

    Elegant Overflow with CSS Ellipsis

    Overflow with text is always a big issue, especially in a programmatic environment. There is always only so much space but variable content to add to that space. I recently worked on a table for displaying user information and noticed that longer strings were …

  • Adding events to add events in MooTools

    Adding events to add events in MooTools

    Note: This post has been updated. One of my big web peeves is when an item has click events attached to it, but the item does not have the “cursor” cursor. I mean how the hell should the user know that they can / should click on …

William

Leave a Reply

Your email address will not be published.