Encodera

Thanks for reading!

Donate to the site — we work hard for you.

JavaScript reference

crypto.randomUUID

Creates a random unique identifier (UUID).

Available since: Web API

Syntax

crypto.randomUUID()

Return value

stringA random version 4 UUID of 36 characters.

Examples

A new identifier:

console.log(crypto.randomUUID());

Output:

3b241101-e2bb-4255-8caf-4136c566a962

Ids for new records:

const todo = { id: crypto.randomUUID(), title: 'Buy tea' };
 
console.log(todo.id.length);

Output:

36

Notes

The value is different every time and the chance of a collision is negligible. In browsers the method works only on HTTPS pages and on localhost.

See also

  • Math.random — Returns a random number between 0 and 1.
  • Date.now — Returns the current time as a number of milliseconds.

Related recipes

In the tutorial

  • Numbers — Numbers in JavaScript — integers and fractions, rounding, the Math object, toFixed and the special values Infinity and NaN.

Thanks for reading!

Donate to the site — we work hard for you.